uifw/AvKon/src/AknQueryValueDuration.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 // AknQueryValueDuration.cpp
       
    19 //
       
    20 // Copyright (c) 2001 Symbian Ltd.  All rights reserved.
       
    21 //
       
    22 
       
    23 #include "eikenv.h"
       
    24 
       
    25 #include <eikmfne.h>
       
    26 #include <avkon.hrh>
       
    27 #include <avkon.rsg>
       
    28 #include "AknQueryValueDuration.h"
       
    29 #include "AknQueryDialog.h"
       
    30 #include "aknmfnesettingpage.h"
       
    31 
       
    32 #include "AknPanic.h"
       
    33 
       
    34 //-------------------------------------------------
       
    35 // class CAknQueryValueDuration
       
    36 //-------------------------------------------------
       
    37 
       
    38 /**
       
    39  * First stage of two stage construction.
       
    40  */
       
    41 EXPORT_C CAknQueryValueDuration* CAknQueryValueDuration::NewL()
       
    42 	{
       
    43 	CAknQueryValueDuration* self = NewLC();
       
    44 	CleanupStack::Pop();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 /**
       
    49  * First stage of two stage construction.
       
    50  */
       
    51 EXPORT_C CAknQueryValueDuration* CAknQueryValueDuration::NewLC()
       
    52 	{
       
    53 	CAknQueryValueDuration* self = new(ELeave) CAknQueryValueDuration;
       
    54 	CleanupStack::PushL(self);
       
    55 	self->ConstructL();
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 /**
       
    60  * Destructor.
       
    61  */
       
    62 EXPORT_C CAknQueryValueDuration::~CAknQueryValueDuration()
       
    63 	{
       
    64 	}
       
    65 
       
    66 /**
       
    67  * Set the array.
       
    68  * 
       
    69  * @param aArray pointer to array, ownership is not passed
       
    70  */
       
    71 EXPORT_C void CAknQueryValueDuration::SetArrayL(const CAknQueryValueDurationArray* aArray)
       
    72 	{
       
    73 	iArray = aArray;
       
    74 	}
       
    75 
       
    76 /**
       
    77  * Set the string used for the query caption to be a string other than the default.
       
    78  *
       
    79  * @param aResourceId		resource id of string to use for caption.
       
    80  *
       
    81  */
       
    82 EXPORT_C void CAknQueryValueDuration::SetQueryCaption(TInt aResourceId)
       
    83 	{
       
    84 	iQueryCaptionId = aResourceId;
       
    85 	}
       
    86 
       
    87 /**
       
    88  * Return the current value, which may have been set by the user
       
    89  *
       
    90  * @return The current value
       
    91  *
       
    92  */
       
    93 EXPORT_C TTimeIntervalSeconds CAknQueryValueDuration::Value() const
       
    94 	{
       
    95 	return iDuration;
       
    96 	}
       
    97 
       
    98 /**
       
    99  * Returns the array as a descriptor array
       
   100  * 
       
   101  * @return descriptor array, ownership is not passed
       
   102  */
       
   103 EXPORT_C const MDesCArray* CAknQueryValueDuration::MdcArray() const
       
   104 	{
       
   105 	return iArray;
       
   106 	}
       
   107 
       
   108 /**
       
   109  * Returns the current value as text
       
   110  * 
       
   111  * @return	descriptor representing current value, new 
       
   112  * descriptor is created and left on cleanup stack, ownership passed back to client
       
   113  */
       
   114 EXPORT_C HBufC* CAknQueryValueDuration::CurrentValueTextLC()
       
   115 	{
       
   116 	HBufC* buf = HBufC::NewMaxLC(iArray->FormattedStringSize());
       
   117 	TPtr ptr = buf->Des();
       
   118 	ptr.Format(*(iArray->FormatString()), iDuration.Int());
       
   119 		iArray->FormatDuration(ptr, iDuration);
       
   120     AknTextUtils::DisplayTextLanguageSpecificNumberConversion(ptr);
       
   121 	return buf;
       
   122 	}
       
   123 
       
   124 /**
       
   125  * Returns the index in the array of the current value. 
       
   126  * If there are duplicates, returns the index of the first match.
       
   127  * If there are no matches, returns zero;
       
   128  *
       
   129  * @return index in array of current value
       
   130  */
       
   131 EXPORT_C TInt CAknQueryValueDuration::CurrentValueIndex() const
       
   132 	{
       
   133 	return iCurrentIndex;
       
   134 	}
       
   135 
       
   136 /**
       
   137  * Changes the current value to correspond to a value in the array.
       
   138  *
       
   139  * @param aIndex	index in array of value to set as current
       
   140  */
       
   141 EXPORT_C void CAknQueryValueDuration::SetCurrentValueIndex(const TInt aIndex)
       
   142 	{
       
   143 	iCurrentIndex = aIndex;
       
   144 	const CAknQueryValueDurationArray::DurationArray* durationArray = iArray->Array();
       
   145 	if (IsValidIndex(aIndex))
       
   146 	    iDuration = (*durationArray)[aIndex];
       
   147 	}
       
   148 
       
   149 /**
       
   150  * Creates a dialog containing a query control. If the value is edited and the Dialog
       
   151  * OK'd, the new value will be set as the current value. Otherwise the current value 
       
   152  * remains unchanged.
       
   153  *
       
   154  * @return	ETrue if current value was altered; EFalse otherwise
       
   155  */
       
   156 EXPORT_C TBool CAknQueryValueDuration::CreateEditorL()
       
   157 	{
       
   158 	TBool result = EFalse;
       
   159 	TInt keyPressed;
       
   160 
       
   161 	HBufC* queryString = CEikonEnv::Static()->AllocReadResourceLC(iQueryCaptionId);
       
   162 
       
   163 	if ( iFlags.IsSet( ESettingPageModeBitIndex ) )
       
   164 		{
       
   165 		CAknDurationSettingPage* dlg =  new ( ELeave ) CAknDurationSettingPage ( queryString, 
       
   166 			EAknSettingPageNoOrdinalDisplayed, EEikCtDurationEditor,
       
   167 			R_AVKON_DEFAULT_SETTING_PAGE_DURATION_EDITOR, 0, iDuration );
       
   168 		result =  dlg->ExecuteLD( );
       
   169 
       
   170 		if ( result )
       
   171 			keyPressed = EAknSoftkeyOk;
       
   172 		else
       
   173 			keyPressed = EAknSoftkeyCancel;
       
   174 		}
       
   175 	else
       
   176 		{
       
   177 		CAknDurationQueryDialog* dlg = CAknDurationQueryDialog::NewL(iDuration);
       
   178 		keyPressed = dlg->ExecuteLD(R_AVKON_DIALOG_QUERY_VALUE_DURATION,*queryString);
       
   179 		}
       
   180 	
       
   181 	CleanupStack::PopAndDestroy(); // queryString
       
   182 
       
   183 	switch(keyPressed)
       
   184 		{
       
   185 	case EAknSoftkeyOk:
       
   186 		result = ETrue;
       
   187 		if ( iFlags.IsSet( EAutoAppendBitIndex ) )
       
   188 			{
       
   189 			AppendValueIfNewL();
       
   190 			}
       
   191 		CalculateCurrentIndex();
       
   192 		break;
       
   193 	case EAknSoftkeyCancel:
       
   194 		// fall through
       
   195 	case EAknSoftkeyBack:
       
   196 		// fall through
       
   197 	default:
       
   198 		result = EFalse;
       
   199 		break;
       
   200 		}
       
   201 
       
   202 	return result;
       
   203 	}
       
   204 
       
   205 /**
       
   206  * Two stage construction
       
   207  *
       
   208  */
       
   209 void CAknQueryValueDuration::ConstructL()
       
   210 	{
       
   211 	iQueryCaptionId = R_AVKON_TBUF_POPUP_FIELD_DEFAULT_DURATION_QUERY_PROMPT;
       
   212 	}
       
   213 
       
   214 /**
       
   215  * Constructor. Does nothing.
       
   216  *
       
   217  */
       
   218 CAknQueryValueDuration::CAknQueryValueDuration()
       
   219 	{
       
   220 	}
       
   221 
       
   222 /**
       
   223  * Search for the current value in the list, and set the curent index 
       
   224  * to be the found index. If not found, set current index to 1 after the size of the array.
       
   225  *
       
   226  */
       
   227 void CAknQueryValueDuration::CalculateCurrentIndex()
       
   228 	{
       
   229 	// ensure that if the new value is not in the list, the current index is set to the beginning
       
   230 	TInt index = 0; 
       
   231 	const CAknQueryValueDurationArray::DurationArray* durationArray = iArray->Array();
       
   232 	TInt length = durationArray->Count();
       
   233 	// search through array to find match for current value
       
   234 	TBool match = EFalse;
       
   235 	for(TInt i = 0; i < length; i++)
       
   236 		{
       
   237 		if((*durationArray)[i] == iDuration)
       
   238 			{
       
   239 			index = i;
       
   240 			match = ETrue;
       
   241 			break;
       
   242 			}
       
   243 		}
       
   244 	if(!match)
       
   245 		index = length;
       
   246 	iCurrentIndex = index;
       
   247 	}
       
   248 
       
   249 /**
       
   250  * Append the current value if it is new
       
   251  */
       
   252 void CAknQueryValueDuration::AppendValueIfNewL()
       
   253 	{
       
   254 	const CAknQueryValueDurationArray::DurationArray* array = iArray->Array();
       
   255 	CalculateCurrentIndex();
       
   256 	if ( iCurrentIndex == array->Count() )
       
   257 		{
       
   258 		CAknQueryValueDurationArray::DurationArray* array = iArray->Array();
       
   259 		array->AppendL( iDuration );
       
   260 		}
       
   261 	}
       
   262 
       
   263 EXPORT_C void CAknQueryValueDuration::Reserved_MAknQueryValue()
       
   264 	{
       
   265 
       
   266 	};
       
   267 
       
   268 
       
   269 //-------------------------------------------------
       
   270 // class CAknQueryValueDurationArray
       
   271 //-------------------------------------------------
       
   272 
       
   273 /**
       
   274  * NewL.
       
   275  *
       
   276  * @param aResourceId	Recource id of a TBUF containing a duration format string. Client
       
   277  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_DURATION_FORMAT
       
   278  *							if desired, or provide its own resource. (must be at least "%d%d%d")
       
   279  */
       
   280 EXPORT_C CAknQueryValueDurationArray* CAknQueryValueDurationArray::NewL(TInt aResourceId)
       
   281 	{
       
   282 	CAknQueryValueDurationArray* self = NewLC(aResourceId);
       
   283 	CleanupStack::Pop();
       
   284 	return self;
       
   285 	}
       
   286 
       
   287 /**
       
   288  * NewLC.
       
   289  *
       
   290  * @param aResourceId	Recource id of a TBUF containing a duration format string. Client
       
   291  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_DURATION_FORMAT
       
   292  *							if desired, or provide its own resource. (must be at least "%d%d%d")
       
   293  */
       
   294 EXPORT_C CAknQueryValueDurationArray* CAknQueryValueDurationArray::NewLC(TInt aResourceId)
       
   295 	{
       
   296 	CAknQueryValueDurationArray* self = new(ELeave) CAknQueryValueDurationArray;
       
   297 	CleanupStack::PushL(self);
       
   298 	self->ConstructL(aResourceId);
       
   299 	return self;
       
   300 	}
       
   301 
       
   302 /**
       
   303  * Destructor.
       
   304  */
       
   305 EXPORT_C CAknQueryValueDurationArray::~CAknQueryValueDurationArray()
       
   306 	{
       
   307 	delete iLastGeneratedTextValue;
       
   308 	delete iFormatString;
       
   309 	}
       
   310 
       
   311 /**
       
   312  * Set the array of values. 
       
   313  * Note that client can use any implementation of array class, but must pass in a 
       
   314  * TArray generated from it (by calling the Array() method on the array class)
       
   315  *
       
   316  * @param array of values, ownership is not passed
       
   317  */
       
   318 EXPORT_C void CAknQueryValueDurationArray::SetArray(DurationArray& aArray)
       
   319 	{
       
   320 	iArray = &aArray;
       
   321 	}
       
   322 
       
   323 /**
       
   324  * Get the array of values as a TArray. 
       
   325  * Note that client can use any implementation of array class, but the array is 
       
   326  * treated as a TArray.
       
   327  *
       
   328  * @return array of values, ownership is not passed
       
   329  */
       
   330 EXPORT_C CAknQueryValueDurationArray::DurationArray* CAknQueryValueDurationArray::Array() const
       
   331 	{
       
   332 	return iArray;
       
   333 	}
       
   334 
       
   335 /**
       
   336  * Return the TTime format string that is used to generate MDesCArray values
       
   337  *
       
   338  * @return format string
       
   339  */
       
   340 EXPORT_C const HBufC* CAknQueryValueDurationArray::FormatString() const
       
   341 	{
       
   342 	return iFormatString;
       
   343 	}
       
   344 
       
   345 /**
       
   346  * Return the size of a maximal time string formated using the format string 
       
   347  * that was supplied during construction of this instance.
       
   348  * Two versions to eliminate compiler warnings.
       
   349  *
       
   350  * @return length of formatted string.
       
   351  */
       
   352 #ifdef __WINS__
       
   353 EXPORT_C const TInt CAknQueryValueDurationArray::FormattedStringSize() const
       
   354 	{
       
   355 	return iFormattedStringSize;
       
   356 	}
       
   357 #else
       
   358 EXPORT_C TInt CAknQueryValueDurationArray::FormattedStringSize() const
       
   359 	{
       
   360 	return iFormattedStringSize;
       
   361 	}
       
   362 #endif // __WINS__
       
   363 
       
   364 /**
       
   365  * Format a descriptor with the textual version of a duration, formated according to the format string. 
       
   366  * Number of hours is not bounded.
       
   367  *
       
   368  * @param aText descriptor to be filled, must be of sufficient size
       
   369  * @param aDuration the interval to be formatted
       
   370  *
       
   371  */
       
   372 EXPORT_C void CAknQueryValueDurationArray::FormatDuration(TPtr& aText, TTimeIntervalSeconds aDuration) const
       
   373 	{
       
   374 	TInt timeIntervalSeconds=aDuration.Int();
       
   375 	TInt second=timeIntervalSeconds%60;
       
   376 	timeIntervalSeconds/=60;
       
   377 	TInt minute=timeIntervalSeconds%60;
       
   378 	timeIntervalSeconds/=60;
       
   379 	TInt hour=timeIntervalSeconds%24;
       
   380 
       
   381 	aText.Format(iFormatString->Des(), hour, minute, second);
       
   382     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( aText );
       
   383 	}
       
   384 
       
   385 /**
       
   386  * Reports count of contained array
       
   387  *
       
   388  * @return count of contained array
       
   389  */
       
   390 EXPORT_C TInt CAknQueryValueDurationArray::MdcaCount() const
       
   391 	{
       
   392 	return iArray->Count();
       
   393 	}
       
   394 
       
   395 /**
       
   396  * Returns array element, converting value to text. 
       
   397  * <p> WARNING: the returned pointer is only valid until the next time this
       
   398  * method is invoked. 
       
   399  *
       
   400  * @param index of element to return
       
   401  * @return descriptor representing array element, ownership is not passed
       
   402  */
       
   403 EXPORT_C TPtrC CAknQueryValueDurationArray::MdcaPoint(TInt aIndex) const
       
   404 	{
       
   405 	// only storing the last used string saves memory by avoiding keeping an array 
       
   406 	// of descriptors in memory. 
       
   407 	TTimeIntervalSeconds duration = (*iArray)[aIndex];
       
   408 
       
   409 	TPtr textPtr = iLastGeneratedTextValue->Des();
       
   410 	FormatDuration(textPtr, duration);
       
   411 	return textPtr;
       
   412 	}
       
   413 
       
   414 /**
       
   415  * Constructor.
       
   416  */
       
   417 CAknQueryValueDurationArray::CAknQueryValueDurationArray()
       
   418 	{
       
   419 
       
   420 	}
       
   421 
       
   422 /**
       
   423  * ConstructL
       
   424  *
       
   425  * @param aResourceId	id of a resource containing a time format string
       
   426  */
       
   427 void CAknQueryValueDurationArray::ConstructL(TInt aResourceId)
       
   428 	{
       
   429 	iFormatString = CEikonEnv::Static()->AllocReadResourceL(aResourceId);
       
   430 	iFormattedStringSize = KSafeSizeOfDescriptorForDurationFormat;
       
   431 	iLastGeneratedTextValue = HBufC::NewL(iFormattedStringSize);
       
   432 	}
       
   433 
       
   434 // End of File