|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : LbsReferenceContainer.cpp 00004 * Part of : LBSReference example application 00005 * Interface : 00006 * Description : This class functions as view of the application 00007 * Version : 1.0 00008 * 00009 * Copyright (c) 2005-2006 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================== 00014 */ 00015 00016 // INCLUDE FILES 00017 #include <eikedwin.h> 00018 #include <eikappui.h> 00019 #include <aknutils.h> 00020 #include <AknAppUi.h> 00021 #include <aknlists.h> 00022 #include <barsread.h> 00023 #include <aknnotewrappers.h> 00024 #include "LBSPositionRequestor.h" 00025 #include <LbsReference.rsg> 00026 #include "LBSReferenceContainer.h" 00027 00028 // CONSTANTS 00029 00030 // Degrees sign delimeter used in formatting methods 00031 _LIT(KDelimDegree,"\xb0"); // "°" symbol 00032 00033 // Dot delimeter used in formatting methods 00034 _LIT(KDelimDot,"\x2e"); // "." symbol 00035 00036 // Plus sign delimeter used in formatting methods 00037 _LIT(KDelimPlus,"\x2b"); // "+" symbol 00038 00039 // Minus sign delimeter used in formatting methods 00040 _LIT(KDelimMinus,"\x2d"); // "-" symbol 00041 00042 // Quotation sign delimeter used in formatting methods 00043 _LIT(KDelimQuot,"\x22"); // "\"" symbol 00044 00045 // Apostrophe sign delimeter used in formatting methods 00046 _LIT(KApostrophe,"\x27"); // "'" symbol 00047 00048 // Not-a-number string 00049 _LIT(KNan,"NaN"); 00050 00051 // Date format 00052 _LIT(KDateString,"%F%D.%M.%Y %:0%H%:1%T%:2%S%:3"); 00053 00054 // String constants 00055 _LIT(KTxtModuleName, "Module name"); 00056 _LIT(KTxtLatitude, "Latitude"); 00057 _LIT(KTxtLongitude, "Longitude"); 00058 _LIT(KTxtAltitude, "Altitude"); 00059 _LIT(KTxtHRMS, "HRMS"); 00060 _LIT(KTxtVRMS, "VRMS"); 00061 _LIT(KTxtTime, "Time"); 00062 _LIT(KTxtSpeed, "Speed"); 00063 _LIT(KTxtSpeedAccuracy, "Speed accuracy"); 00064 _LIT(KTxtHeading, "Heading"); 00065 _LIT(KTxtHeadingAccuracy, "Heading Accuracy"); 00066 _LIT(KTxtSatelliteTime, "Satellite time"); 00067 _LIT(KTxtSatellitesInView, "Satellites in view"); 00068 _LIT(KTxtSatellitesUsed, "Satellites used"); 00069 00070 // ================= MEMBER FUNCTIONS ======================= 00071 00072 CLbsReferenceContainer::CLbsReferenceContainer() 00073 : iListBox(NULL), iItemArray(NULL), iValueBuffer(), iListItemBuffer(), 00074 iModuleName(), iPositionRequestor(NULL) 00075 { 00076 } 00077 00078 00079 00080 // --------------------------------------------------------- 00081 // CLbsReferenceContainer::ConstructL(const TRect& aRect) 00082 // Symbian OS two phased constructor 00083 // --------------------------------------------------------- 00084 // 00085 void CLbsReferenceContainer::ConstructL(const TRect& aRect) 00086 { 00087 TResourceReader rr; 00088 00089 // Create a window for this application view 00090 CreateWindowL(); 00091 00092 iEikonEnv->CreateResourceReaderLC(rr, R_LOC_LISTBOX_RESOURCE); 00093 00094 iListBox = new (ELeave) CAknDoubleStyleListBox (); 00095 iListBox->SetContainerWindowL( *this); 00096 iListBox->ConstructFromResourceL( rr ); 00097 CleanupStack::PopAndDestroy(); // resource stuffs. 00098 00099 CEikScrollBarFrame* sb = iListBox->CreateScrollBarFrameL(); 00100 sb->SetScrollBarVisibilityL (CEikScrollBarFrame::EOn, CEikScrollBarFrame::EOn); 00101 00102 MDesCArray* itemList = iListBox->Model()->ItemTextArray(); 00103 iItemArray = (CDesCArray*) itemList; 00104 00105 // Set the windows size 00106 SetRect(aRect); 00107 00108 // Activate the window, which makes it visible 00109 ActivateL(); 00110 00111 // Create and initialise position requestor 00112 iPositionRequestor = CLbsPositionRequestor::NewL(*this); 00113 } 00114 00115 // ---------------------------------------------------- 00116 // CLbsReferenceContainer::~CLbsReferenceContainer() 00117 // Destructor 00118 // Frees reserved resources 00119 // ---------------------------------------------------- 00120 // 00121 CLbsReferenceContainer::~CLbsReferenceContainer() 00122 { 00123 //Delete text control 00124 delete iListBox; 00125 00126 //Delete requestor 00127 delete iPositionRequestor; 00128 } 00129 00130 00131 00132 void CLbsReferenceContainer::AddItemInListL(const TDesC& aLabel, const TDesC& aValue) 00133 { 00134 _LIT(KTab, "\t"); 00135 iListItemBuffer.Zero(); 00136 iListItemBuffer.Append(KTab); 00137 iListItemBuffer.Append(aLabel); 00138 iListItemBuffer.Append(KTab); 00139 iListItemBuffer.Append(aValue); 00140 00141 iItemArray->AppendL(iListItemBuffer); 00142 } 00143 00144 // --------------------------------------------------------- 00145 // CLbsReferenceAppUi::PositionInfoUpdatedL() 00146 // implements MPositionListener::PositionInfoUpdatedL 00147 // Processes given position information and displays it to user 00148 // This method handles TPositionInfo and TPositionSatelliteInfo 00149 // --------------------------------------------------------- 00150 // 00151 void CLbsReferenceContainer::PositionInfoUpdatedL(TPositionInfoBase& aPosInfo, 00152 const TDesC& aModulename) 00153 { 00154 TInt count = iItemArray->Count(); 00155 iItemArray->Reset(); 00156 00157 // Set the module name 00158 iModuleName = aModulename; 00159 AddItemInListL(KTxtModuleName, iModuleName); 00160 00161 // Check the type of the updated position information 00162 // and process that information. 00163 00164 // Check if position information class type is TPositionSatelliteInfo 00165 if (aPosInfo.PositionClassType() & EPositionSatelliteInfoClass) 00166 { 00167 // Cast the TPositionInfoBase object to TPositionSatelliteInfo 00168 TPositionSatelliteInfo* posInfo = static_cast<TPositionSatelliteInfo*>(&aPosInfo); 00169 00170 // Process the satellite information 00171 ProcessSatelliteInfoL( *posInfo ); 00172 00173 } 00174 // Check if position information class type is TPositionInfo 00175 else if (aPosInfo.PositionClassType() & EPositionInfoClass) 00176 { 00177 00178 //Cast the TPositionInfoBase object to TPositionInfo 00179 TPositionInfo* posInfo = static_cast<TPositionInfo*>(&aPosInfo); 00180 00181 //Process the position information 00182 ProcessPositionInfoL( *posInfo ); 00183 00184 } 00185 if (count < iItemArray->Count()) 00186 { 00187 iListBox->HandleItemAdditionL(); 00188 } 00189 else if (count > iItemArray->Count()) 00190 { 00191 iListBox->HandleItemRemovalL(); 00192 } 00193 else 00194 { 00195 iListBox->DrawNow(); 00196 } 00197 } 00198 00199 // --------------------------------------------------------- 00200 // CLbsReferenceAppUi::ShowErrorL 00201 // Implements MPositionListener::ShowErrorL 00202 // Informs the listener about occurred error during position 00203 // request process 00204 // @param aErrorString The error in string format 00205 // --------------------------------------------------------- 00206 // 00207 void CLbsReferenceContainer::ShowErrorL(const TDesC& aErrorString) 00208 { 00209 TInt count = iItemArray->Count(); 00210 iItemArray->Reset(); 00211 00212 _LIT( KLocError, "Location error"); 00213 AddItemInListL( KLocError, aErrorString); 00214 00215 if (iItemArray->Count() < count) 00216 { 00217 iListBox->HandleItemRemovalL(); 00218 } 00219 iListBox->DrawNow(); 00220 } 00221 00222 // --------------------------------------------------------- 00223 // CLbsReferenceAppUi::ShowMessageL 00224 // Implements MPositionListener::ShowMessageL 00225 // Informs the listener about occurred error during position 00226 // request process 00227 // @param aMessage The event in string format 00228 // --------------------------------------------------------- 00229 // 00230 void CLbsReferenceContainer::ShowMessageL(const TDesC& aMessage) 00231 { 00232 CAknInformationNote* note = new (ELeave) CAknInformationNote(); 00233 note->SetTextL(aMessage); 00234 note->ExecuteLD(); 00235 } 00236 00237 00238 // --------------------------------------------------------- 00239 // CLbsReferenceContainer::SizeChanged() 00240 // Called by framework when the view size is changed 00241 // --------------------------------------------------------- 00242 // 00243 void CLbsReferenceContainer::SizeChanged() 00244 { 00245 // Make control use the whole client area 00246 iListBox->SetRect(Rect()); 00247 } 00248 00249 // --------------------------------------------------------- 00250 // CLbsReferenceContainer::CountComponentControls() const 00251 // --------------------------------------------------------- 00252 // 00253 TInt CLbsReferenceContainer::CountComponentControls() const 00254 { 00255 return 1; // return nbr of controls inside this container 00256 } 00257 00258 // --------------------------------------------------------- 00259 // CLbsReferenceContainer::ComponentControl(TInt aIndex) const 00260 // --------------------------------------------------------- 00261 // 00262 CCoeControl* CLbsReferenceContainer::ComponentControl(TInt aIndex) const 00263 { 00264 CCoeControl* control = NULL; 00265 switch ( aIndex ) 00266 { 00267 // Return label control 00268 case 0: 00269 control = iListBox; 00270 break; 00271 // return NULL, no components 00272 default: 00273 control = NULL; 00274 } 00275 return control; 00276 } 00277 00278 // --------------------------------------------------------- 00279 // CLbsReferenceContainer::Draw(const TRect& aRect) const 00280 // --------------------------------------------------------- 00281 // 00282 void CLbsReferenceContainer::Draw(const TRect& aRect) const 00283 { 00284 //Get the graphics context 00285 CWindowGc& gc = SystemGc(); 00286 00287 //Set pen style 00288 gc.SetPenStyle(CGraphicsContext::ENullPen); 00289 00290 //Set brush color 00291 gc.SetBrushColor(KRgbWhite); 00292 00293 //Set brush style 00294 gc.SetBrushStyle(CGraphicsContext::ESolidBrush); 00295 00296 //Draw the window area 00297 gc.DrawRect(aRect); 00298 } 00299 00300 // ----------------------------------------------------------------------------- 00301 // CLbsReferenceContainer::OfferKeyEventL 00302 // Handles key events. Basically it just forwards them to listbox component. 00303 // ----------------------------------------------------------------------------- 00304 // 00305 TKeyResponse CLbsReferenceContainer::OfferKeyEventL(const TKeyEvent &aKeyEvent, TEventCode aType) 00306 { 00307 return iListBox->OfferKeyEventL(aKeyEvent, aType); 00308 } 00309 00310 // ----------------------------------------------------------------------------- 00311 // CLbsReferenceContainer::ProcessPositionInfo 00312 // Processes position info in to a string representation. 00313 // ----------------------------------------------------------------------------- 00314 // 00315 void CLbsReferenceContainer::ProcessPositionInfoL( 00316 const TPositionInfo& aPositionInfo ) 00317 { 00318 const TInt KRealNumberFigureAmount = 2; 00319 const TInt KRealNumberWidth = 5; 00320 00321 // Standard location information 00322 TPosition position; 00323 TRealFormat format; 00324 format.iPlaces = KRealNumberFigureAmount; 00325 format.iWidth = KRealNumberWidth; 00326 00327 // Get basic position data 00328 aPositionInfo.GetPosition(position); 00329 00330 // Get the latitude and format it 00331 TBuf<KDegreeLength> latitude; 00332 GetDegreesString(position.Latitude(), latitude); 00333 00334 // Get the longitude and format it 00335 TBuf<KDegreeLength> longitude; 00336 GetDegreesString(position.Longitude(), longitude); 00337 00338 //Get the altitude 00339 TReal altitude = position.Altitude(); 00340 00341 // Get the horizontal accuracy 00342 TReal horizontalAccuracy = position.HorizontalAccuracy(); 00343 00344 // Get the vertical accuracy 00345 TReal verticalAccuracy = position.VerticalAccuracy(); 00346 00347 // Get the timestamp of the fix 00348 TTime time = position.Time(); 00349 00350 // Temporary buffer for date formatting 00351 TBuf<KDateTimeLength> positionDateTime; 00352 00353 //Format dates and times 00354 time.FormatL(positionDateTime, KDateString); 00355 00356 // Clear the buffer 00357 iValueBuffer.Zero(); 00358 00359 AddItemInListL(KTxtLatitude, latitude); 00360 AddItemInListL(KTxtLongitude, longitude); 00361 00362 iValueBuffer.Zero(); 00363 iValueBuffer.AppendNum(altitude, format); 00364 AddItemInListL(KTxtAltitude, iValueBuffer); 00365 00366 iValueBuffer.Zero(); 00367 iValueBuffer.AppendNum(horizontalAccuracy, format); 00368 AddItemInListL(KTxtHRMS, iValueBuffer); 00369 00370 iValueBuffer.Zero(); 00371 iValueBuffer.AppendNum(verticalAccuracy, format); 00372 AddItemInListL(KTxtVRMS, iValueBuffer); 00373 00374 iValueBuffer.Zero(); 00375 iValueBuffer.Append(positionDateTime); 00376 AddItemInListL(KTxtTime, positionDateTime); 00377 00378 } 00379 00380 // ----------------------------------------------------------------------------- 00381 // CLbsReferenceContainer::ProcessPositionInfo 00382 // Processes satellite position info in to a string representation. 00383 // ----------------------------------------------------------------------------- 00384 // 00385 00386 void CLbsReferenceContainer::ProcessSatelliteInfoL( 00387 const TPositionSatelliteInfo& aSatelliteInfo ) 00388 { 00389 const TReal SpeedRatio = 36.0/10.0; 00390 const TInt KRealNumberFigureAmount = 2; 00391 const TInt KRealNumberWidth = 5; 00392 00393 // Process the basic info 00394 ProcessPositionInfoL(static_cast<TPositionInfo>(aSatelliteInfo)); 00395 00396 // The course info 00397 TCourse course; 00398 00399 // Get the satellite info 00400 aSatelliteInfo.GetCourse(course); 00401 00402 // Get the speed (m/s) 00403 TReal speed = course.Speed(); 00404 00405 // Convert to km/h if the speedAccuracy is not NaN 00406 if ( !Math::IsNaN(STATIC_CAST(TReal64, speed)) ) 00407 { 00408 // Convert to km/h 00409 speed = speed * SpeedRatio; 00410 } 00411 00412 // Get the speed accuracy (m/s) 00413 TReal speedAccuracy = course.SpeedAccuracy(); 00414 00415 // Convert to km/h if the speedAccuracy is not NaN 00416 if ( !Math::IsNaN(speedAccuracy) ) 00417 { 00418 // Convert to km/h 00419 speedAccuracy = speedAccuracy * SpeedRatio; 00420 } 00421 00422 // Get the heading (degrees) 00423 TReal heading = course.Heading(); 00424 00425 // Get the heading accuracy (degrees) 00426 TReal headingAccuracy = course.HeadingAccuracy(); 00427 00428 // Get the satellite time 00429 TTime time = aSatelliteInfo.SatelliteTime(); 00430 00431 // Format the date 00432 TBuf<KDateTimeLength> satelliteDateTime; 00433 00434 //Format dates and times 00435 time.FormatL(satelliteDateTime,KDateString); 00436 00437 // Get the number of visible satellites 00438 TInt numSatellitesInView = aSatelliteInfo.NumSatellitesInView(); 00439 00440 // Get the number of satellites utilized 00441 TInt numSatellitesUsed = aSatelliteInfo.NumSatellitesUsed(); 00442 00443 TRealFormat format; 00444 format.iPlaces = KRealNumberFigureAmount; 00445 format.iWidth = KRealNumberWidth; 00446 00447 // Append the information in list box item array 00448 iValueBuffer.Zero(); 00449 iValueBuffer.AppendNum(speed, format); 00450 AddItemInListL(KTxtSpeed, iValueBuffer); 00451 00452 iValueBuffer.Zero(); 00453 iValueBuffer.AppendNum(speedAccuracy, format); 00454 AddItemInListL(KTxtSpeedAccuracy, iValueBuffer); 00455 00456 iValueBuffer.Zero(); 00457 iValueBuffer.AppendNum(heading, format); 00458 AddItemInListL(KTxtHeading, iValueBuffer); 00459 00460 iValueBuffer.Zero(); 00461 iValueBuffer.AppendNum(headingAccuracy, format); 00462 AddItemInListL(KTxtHeadingAccuracy, iValueBuffer); 00463 00464 AddItemInListL(KTxtSatelliteTime, satelliteDateTime); 00465 00466 TInt64 value = numSatellitesInView; 00467 iValueBuffer.Zero(); 00468 iValueBuffer.AppendNum(value); 00469 AddItemInListL(KTxtSatellitesInView, iValueBuffer); 00470 00471 value = numSatellitesUsed; 00472 iValueBuffer.Zero(); 00473 iValueBuffer.AppendNum(value); 00474 AddItemInListL(KTxtSatellitesUsed, iValueBuffer); 00475 00476 } 00477 00478 // ----------------------------------------------------------------------------- 00479 // CLbsReferenceContainer::GetDegreesString 00480 // Changes latitude or longitude represented as degrees to a string of 00481 // a form +DDD'MM''SS.SSSS 00482 // ----------------------------------------------------------------------------- 00483 // 00484 00485 void CLbsReferenceContainer::GetDegreesString( 00486 const TReal64& aDegrees,TBuf<KDegreeLength>& aDegreesString) const 00487 { 00488 const TReal KSecondsInMinute = 60.0; 00489 const TInt KNumWidth = 3; 00490 00491 // If the aDegree is a proper number 00492 if ( !Math::IsNaN(aDegrees) ) 00493 { 00494 // Integer part of the degrees 00495 TInt intDegrees = static_cast<TInt>(aDegrees); 00496 00497 // Positive float of the degrees 00498 TReal64 realDegrees = aDegrees; 00499 00500 // Convert to positive values 00501 if ( intDegrees < 0 ) 00502 { 00503 intDegrees = -intDegrees; 00504 realDegrees = -realDegrees; 00505 } 00506 00507 // Minutes 00508 TReal64 realMinutes = (realDegrees - intDegrees) * KSecondsInMinute; 00509 00510 // Integer part of the minutes 00511 TInt intMinutes = static_cast<TInt>(realMinutes); 00512 00513 // Seconds 00514 TReal64 realSeconds = (realMinutes - intMinutes) * KSecondsInMinute; 00515 TInt intSeconds = static_cast<TInt>((realMinutes - intMinutes) * KSecondsInMinute); 00516 00517 // Check the sign of the result 00518 if ( aDegrees >= 0 ) 00519 { 00520 aDegreesString.Append(KDelimPlus); 00521 } 00522 else 00523 { 00524 aDegreesString.Append(KDelimMinus); 00525 } 00526 00527 // Add the degrees 00528 TInt64 value = intDegrees; 00529 aDegreesString.AppendNum(value); 00530 00531 // Add the separator 00532 aDegreesString.Append(KDelimDegree); 00533 00534 // Add the minutes 00535 value = intMinutes; 00536 aDegreesString.AppendNum(value); 00537 00538 // Add the separator 00539 aDegreesString.Append(KApostrophe); 00540 00541 // Add the seconds 00542 value = intSeconds; 00543 aDegreesString.AppendNum(value); 00544 00545 // Add the separator 00546 aDegreesString.Append(KDelimQuot); 00547 00548 // Add the separator 00549 aDegreesString.Append(KDelimDot); 00550 00551 // Get six last digits 00552 realSeconds -= intSeconds; 00553 realSeconds *= 1000; 00554 00555 // Add the seconds 00556 aDegreesString.AppendNumFixedWidth(static_cast<TInt>(realSeconds), EDecimal, KNumWidth); 00557 } 00558 else 00559 { 00560 // The conversion can not be done, return NaN 00561 aDegreesString = KNan; 00562 } 00563 } 00564 00565 // ----------------------------------------------------------------------------- 00566 // CLbsReferenceContainer::HandleResourceChange 00567 // Handle Dynamic Screen Size change and Skin Change events and update 00568 // controls accordingly. 00569 // ----------------------------------------------------------------------------- 00570 // 00571 void CLbsReferenceContainer::HandleResourceChange(TInt aType) 00572 { 00573 CCoeControl::HandleResourceChange(aType); 00574 switch( aType ) 00575 { 00576 case KAknsMessageSkinChange: 00577 { 00578 iListBox->DrawNow(); 00579 break; 00580 } 00581 case KEikDynamicLayoutVariantSwitch: 00582 { 00583 SetRect(iEikonEnv->EikAppUi()->ClientRect()); 00584 DrawNow(); 00585 break; 00586 } 00587 default: 00588 { 00589 break; 00590 } 00591 } 00592 } 00593 00594 // End of File
|
© Nokia 2006 |