Nokia N97 SDK
Example Applications Guide

sensorexampleappview.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : sensorexampleappview.cpp
00004 *  Part of     : sensorexample
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2007, 2008 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 <coemain.h>
00018 #include <aknutils.h>
00019 #include <e32base.h>
00020 #include <sensrvaccelerometersensor.h>
00021 #include <sensrvtappingsensor.h>
00022 #include <aknlistquerydialog.h>
00023 #include <sensorexample.rsg>
00024 #include <sensrvgeneralproperties.h>
00025 #include "sensorexampleappview.h"
00026 
00027 _LIT( KHexPrefix, "0x" );
00028 const TInt KItemBufferSize( 25 );
00029 const TInt KBallDiameter( 60 );
00030 const TInt KEyeDiameter( 10 );
00031 const TInt KMouthWidth( 10 );
00032 const TSize KEyeSize( KEyeDiameter, KEyeDiameter );
00033 const TSize KBallSize( KBallDiameter, KBallDiameter );
00034 const TSize KMouthSizeHorizontal( ( KMouthWidth * 2 ), KMouthWidth );
00035 const TSize KMouthSizeVertical( KMouthWidth, ( KMouthWidth * 2 ) );
00036 
00037 // ============================ MEMBER FUNCTIONS ===============================
00038 
00039 // -----------------------------------------------------------------------------
00040 // CSensorExampleAppView::NewL()
00041 // Two-phased constructor.
00042 // -----------------------------------------------------------------------------
00043 //
00044 CSensorExampleAppView* CSensorExampleAppView::NewL( const TRect& aRect )
00045     {
00046     CSensorExampleAppView* self = CSensorExampleAppView::NewLC( aRect );
00047     CleanupStack::Pop( self );
00048     return self;
00049     }
00050 
00051 // -----------------------------------------------------------------------------
00052 // CSensorExampleAppView::NewLC()
00053 // Two-phased constructor.
00054 // -----------------------------------------------------------------------------
00055 //
00056 CSensorExampleAppView* CSensorExampleAppView::NewLC( const TRect& aRect )
00057     {
00058     CSensorExampleAppView* self = new ( ELeave ) CSensorExampleAppView;
00059     CleanupStack::PushL( self );
00060     self->ConstructL( aRect );
00061     return self;
00062     }
00063 
00064 // -----------------------------------------------------------------------------
00065 // CSensorExampleAppView::ConstructL()
00066 // Symbian 2nd phase constructor can leave.
00067 // -----------------------------------------------------------------------------
00068 //
00069 void CSensorExampleAppView::ConstructL( const TRect& aRect )
00070     {
00071     // Create a window for this application view
00072     CreateWindowL();
00073 
00074     // set the font
00075     iFont = AknLayoutUtils::FontFromId(EAknLogicalFontPrimaryFont);
00076 
00077     // Set the windows size
00078     SetRect( aRect );
00079 
00080     // Activate the window, which makes it ready to be drawn
00081     ActivateL();
00082     }
00083 
00084 // -----------------------------------------------------------------------------
00085 // CSensorExampleAppView::CSensorExampleAppView()
00086 // C++ default constructor can NOT contain any code, that might leave.
00087 // -----------------------------------------------------------------------------
00088 //
00089 CSensorExampleAppView::CSensorExampleAppView()
00090     : iDeviceOrientation( iDeviceOrientation = TSensrvOrientationData::EOrientationDisplayUp ),
00091       iX( 0 ),
00092       iY( 0 ),
00093       iFaceColor( KRgbRed )
00094     {
00095     // No implementation required
00096     }
00097 
00098 // -----------------------------------------------------------------------------
00099 // CSensorExampleAppView::~CSensorExampleAppView()
00100 // Destructor.
00101 // -----------------------------------------------------------------------------
00102 //
00103 CSensorExampleAppView::~CSensorExampleAppView()
00104     {
00105     // No implementation required
00106     }
00107 
00108 
00109 // -----------------------------------------------------------------------------
00110 // CSensorExampleAppView::Draw()
00111 // Draws the display.
00112 // -----------------------------------------------------------------------------
00113 //
00114 void CSensorExampleAppView::Draw( const TRect& /*aRect*/ ) const
00115     {
00116     // note that the whole screen is drawn everytime, so aRect-parameter is ignored
00117     
00118     CWindowGc& gc = SystemGc();// Get the standard graphics context
00119     TRect rect( Rect());// Gets the control's extent
00120     TSize screenSize( rect.Size());
00121     TPoint middlePoint( screenSize.iWidth / 2, screenSize.iHeight / 2);
00122     TPoint ballTopLeftCorner( ( ( middlePoint.iX + iX ) - ( KBallSize.iWidth / 2 ) ),
00123                               ( ( middlePoint.iY  + iY )- ( KBallSize.iHeight / 2 ) ) );
00124     TRect ballRect( ballTopLeftCorner, KBallSize );
00125     
00126     gc.SetPenStyle( CGraphicsContext::ENullPen );
00127     gc.SetBrushColor( KRgbWhite );
00128     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
00129     gc.Clear( rect );//Clear screen
00130     gc.SetBrushColor( iFaceColor );
00131     gc.DrawEllipse( ballRect );//Draw circle for smiley
00132     gc.SetBrushColor( KRgbWhite );
00133     
00134     //Draw rest of smiley i.e. eyes, nose and mouth    
00135     TSize mouthSize;
00136     TRect smileyRect;
00137     TPoint eye1;
00138     TPoint eye2;
00139     TPoint nose;
00140     TPoint mouth;
00141     TPoint eyeTopLeftCorner;
00142     TPoint eyeTopRightCorner;
00143     TPoint eyeBottomLeftCorner;
00144     TPoint eyeBottomRightCorner;
00145     TPoint mouthTop;
00146     TPoint mouthBottom;
00147     TPoint mouthLeft;
00148     TPoint mouthRight;
00149 
00150     eyeTopLeftCorner.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 3 ) - ( KEyeDiameter / 2 );
00151     eyeTopLeftCorner.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 3 ) - ( KEyeDiameter / 2 );
00152     eyeTopRightCorner.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 3 * 2 ) - ( KEyeDiameter / 2 );
00153     eyeTopRightCorner.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 3 ) - ( KEyeDiameter / 2 );
00154     eyeBottomLeftCorner.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 3 ) - ( KEyeDiameter / 2 );
00155     eyeBottomLeftCorner.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 3 * 2 ) - ( KEyeDiameter / 2 );
00156     eyeBottomRightCorner.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 3 * 2 ) - ( KEyeDiameter / 2 );
00157     eyeBottomRightCorner.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 3 * 2 ) - ( KEyeDiameter / 2 );
00158 
00159     mouthTop.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 2 ) - ( KMouthWidth / 2 );
00160     mouthTop.iY = ( ballTopLeftCorner.iY + KMouthWidth / 2 );
00161     mouthBottom.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 2 ) - ( KMouthWidth / 2 );
00162     mouthBottom.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 2 ) + ( KMouthWidth / 2 );
00163     mouthLeft.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 2 ) + ( KMouthWidth / 2 );
00164     mouthLeft.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 2 ) - ( KMouthWidth / 2 );
00165     mouthRight.iX = ( ballTopLeftCorner.iX + KMouthWidth / 2 );
00166     mouthRight.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 2 ) - ( KMouthWidth / 2 );
00167 
00168     nose.iX = ( ballTopLeftCorner.iX + KBallSize.iWidth / 2 ) - ( KMouthWidth / 2 );
00169     nose.iY = ( ballTopLeftCorner.iY + KBallSize.iHeight / 2 ) - ( KMouthWidth / 2 );
00170 
00171     switch( iDeviceOrientation )
00172         {
00173         case TSensrvOrientationData::EOrientationDisplayDown:
00174             {
00175             eye1 = eyeBottomLeftCorner;
00176             eye2 = eyeBottomRightCorner;
00177             mouth = mouthTop;
00178             mouthSize = KMouthSizeVertical;
00179             }
00180             break;
00181 
00182         case TSensrvOrientationData::EOrientationDisplayLeftUp:
00183             {
00184             eye1 = eyeBottomLeftCorner;
00185             eye2 = eyeTopLeftCorner;
00186             mouth = mouthLeft;
00187             mouthSize = KMouthSizeHorizontal;
00188             }
00189             break;
00190 
00191         case TSensrvOrientationData::EOrientationDisplayRightUp:
00192             {
00193             eye1 = eyeTopRightCorner;
00194             eye2 = eyeBottomRightCorner;
00195             mouth = mouthRight;
00196             mouthSize = KMouthSizeHorizontal;
00197             }
00198             break;
00199 
00200         default:
00201             {
00202             eye1 = eyeTopLeftCorner;
00203             eye2 = eyeTopRightCorner;
00204             mouth = mouthBottom;
00205             mouthSize = KMouthSizeVertical;
00206             }
00207             break;
00208         }
00209     
00210     smileyRect.SetRect( eye1, KEyeSize );
00211     gc.DrawEllipse( smileyRect );
00212     smileyRect.SetRect( eye2, KEyeSize );
00213     gc.DrawEllipse( smileyRect );
00214     smileyRect.SetRect( nose, KEyeSize );
00215     gc.DrawEllipse( smileyRect );
00216     smileyRect.SetRect( mouth, mouthSize );
00217     gc.DrawEllipse( smileyRect );
00218     }
00219 
00220 // -----------------------------------------------------------------------------
00221 // CSensorExampleAppView::TappingEvent()
00222 // Called by CSensorExampleDataListener when tapping event occurs
00223 // -----------------------------------------------------------------------------
00224 //
00225 void CSensorExampleAppView::TappingEvent( TInt /*aDirection*/ )
00226     {
00227     //Alter drawing color for smiley
00228     if( KRgbRed == iFaceColor )
00229         {
00230         iFaceColor = KRgbBlue;
00231         }
00232     else
00233         {
00234         iFaceColor = KRgbRed;
00235         }
00236 
00237     DrawNow();
00238     }
00239 
00240 // -----------------------------------------------------------------------------
00241 // CSensorExampleAppView::AccelerometerData
00242 // Called by CSensorExampleDataListener when accelerometer data is available
00243 // -----------------------------------------------------------------------------
00244 //
00245 void CSensorExampleAppView::AccelerometerData( TInt aX, TInt aY )
00246     {
00247     iX = aX;
00248     iY = aY;
00249     DrawNow();
00250     }
00251 
00252 // -----------------------------------------------------------------------------
00253 // CSensorExampleAppView::OrientationEvent
00254 // Called by CSensorExampleDataListener when orientation event occurs
00255 // -----------------------------------------------------------------------------
00256 //
00257 void CSensorExampleAppView::OrientationEvent( 
00258     TSensrvOrientationData::TSensrvDeviceOrientation aDeviceOrientation )
00259     {
00260     if( TSensrvOrientationData::EOrientationUndefined != aDeviceOrientation )
00261         {
00262         iDeviceOrientation = aDeviceOrientation;
00263         DrawNow();
00264         }
00265     }
00266 
00267 // -----------------------------------------------------------------------------
00268 // CSensorExampleAppView::ShowQueryDialogL
00269 // -----------------------------------------------------------------------------
00270 //
00271 TInt CSensorExampleAppView::ShowQueryDialogL( RSensrvChannelInfoList& aChannelList )
00272     {
00273     TInt selectedIndex( 0 );
00274     CAknListQueryDialog* dialog = new (ELeave) CAknListQueryDialog ( &selectedIndex );
00275     dialog->PrepareLC( R_SENSOREXAMPLE_DIALOG_CHANNELTYPEID_QUERY );
00276     CDesCArrayFlat* itemList = new ( ELeave ) CDesCArrayFlat( aChannelList.Count() );
00277     CleanupStack::PushL( itemList );
00278 
00279     for( TInt i=0; i<aChannelList.Count(); i++ )
00280         {
00281         switch( aChannelList[i].iChannelType )
00282             {
00283             case KSensrvChannelTypeIdAccelerometerDoubleTappingData:
00284                 {
00285                 itemList->AppendL( iCoeEnv->AllocReadResourceLC( R_SE_DOUBLETAPCHANNEL )->Des() );
00286                 CleanupStack::PopAndDestroy();//R_SE_DOUBLETAPCHANNEL
00287                 }
00288                 break;
00289 
00290             case KSensrvChannelTypeIdAccelerometerXYZAxisData:
00291                 {
00292                 itemList->AppendL( iCoeEnv->AllocReadResourceLC( R_SE_ACCELEROMETERCHANNEL )->Des() );
00293                 CleanupStack::PopAndDestroy();//R_SE_ACCELEROMETERCHANNEL
00294                 }
00295                 break;
00296 
00297             case  KSensrvChannelTypeIdOrientationData:
00298                 {
00299                 itemList->AppendL( iCoeEnv->AllocReadResourceLC( R_SE_ORIENTATIONCHANNEL )->Des() );
00300                 CleanupStack::PopAndDestroy();//R_SE_ORIENTATIONCHANNEL
00301                 }
00302                 break;
00303 
00304             case  KSensrvChannelTypeIdRotationData:
00305                 {
00306                 itemList->AppendL( iCoeEnv->AllocReadResourceLC( R_SE_ROTATIONCHANNEL )->Des() );
00307                 CleanupStack::PopAndDestroy();//R_SE_ROTATIONCHANNEL
00308                 }
00309                 break;
00310 
00311             default:
00312                 {
00313                 //Only number is printed for the rest of the channels.
00314                 TBuf<KItemBufferSize> item;
00315                 item.Append( KHexPrefix );
00316                 item.AppendNum( aChannelList[i].iChannelType, EHex );
00317                 itemList->AppendL( item );
00318                 }
00319                 break;
00320             }
00321         }
00322 
00323     dialog->SetOwnershipType( ELbmOwnsItemArray );
00324     dialog->SetItemTextArray( itemList );
00325 
00326     CleanupStack::Pop( itemList );
00327 
00328     TInt result = dialog->RunLD();
00329 
00330     if( result == -2 )
00331         {
00332         // Ok was pressed
00333         }
00334     else
00335         {
00336         // Cancel was pressed
00337         selectedIndex = KErrCancel;
00338         }
00339 
00340     return selectedIndex;
00341     }
00342 
00343 // -----------------------------------------------------------------------------
00344 // CSensorExampleAppView::ShowQueryDialogL
00345 // -----------------------------------------------------------------------------
00346 //
00347 TInt CSensorExampleAppView::ShowQueryDialogL( RSensrvPropertyList& aChannelPropertyList )
00348     {
00349     TInt selectedIndex( 0 );
00350     CAknListQueryDialog* dialog = new (ELeave) CAknListQueryDialog ( &selectedIndex );
00351     dialog->PrepareLC( R_SENSOREXAMPLE_DIALOG_CHANNELPROPERTY_QUERY );
00352     CDesCArrayFlat* itemList = new ( ELeave ) CDesCArrayFlat( aChannelPropertyList.Count() );
00353     CleanupStack::PushL( itemList );
00354 
00355     for( TInt i=0; i<aChannelPropertyList.Count(); i++ )
00356         {
00357         TBuf<KItemBufferSize> property;
00358 
00359         GetPropertyStringL( aChannelPropertyList[i].GetPropertyId(),
00360                             aChannelPropertyList[i].PropertyItemIndex(),
00361                             property );
00362 
00363         if( ESensrvArrayPropertyInfo == aChannelPropertyList[i].GetArrayIndex() )
00364             {
00365             //Append suffix for array properties
00366             property.Append( iCoeEnv->AllocReadResourceLC( R_SE_ARRAYSUFFIX )->Des() );
00367             CleanupStack::PopAndDestroy();//R_SE_ARRAYSUFFIX
00368             }
00369         else if( aChannelPropertyList[i].GetArrayIndex() >= 0 )
00370             {
00371             //Append array index
00372             TBuf<KItemBufferSize> buff;
00373             _LIT( KArrayIndex, " (%d)");
00374             buff.Format( KArrayIndex, aChannelPropertyList[i].GetArrayIndex() );
00375             property.Append( buff );
00376             }
00377 
00378         itemList->AppendL( property );
00379         }
00380 
00381     dialog->SetOwnershipType( ELbmOwnsItemArray );
00382     dialog->SetItemTextArray( itemList );
00383 
00384     CleanupStack::Pop( itemList );
00385 
00386     TInt result = dialog->RunLD();
00387 
00388     if( result == -2 )
00389         {
00390         // Ok was pressed
00391         }
00392     else
00393         {
00394         // Cancel was pressed
00395         selectedIndex = KErrCancel;
00396         }
00397 
00398     return selectedIndex;
00399     }
00400 
00401 // -----------------------------------------------------------------------------
00402 // CSensorExampleAppView::GetPropertyStringL
00403 // -----------------------------------------------------------------------------
00404 //
00405 void CSensorExampleAppView::GetPropertyStringL( TSensrvPropertyId aPropertyId,
00406                                                 TInt aItemIndex,
00407                                                 TDes& aPropertyBuf )
00408     {
00409     switch( aPropertyId )
00410         {
00411         case KSensrvPropIdDataRate://0x00000002
00412             {
00413             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_DATARATE )->Des() );
00414             CleanupStack::PopAndDestroy();//R_SE_DATARATE
00415             }
00416             break;
00417 
00418         case KSensrvPropIdAvailability://0x00000004
00419             {
00420             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_AVAILABILITY )->Des() );
00421             CleanupStack::PopAndDestroy();//R_SE_AVAILABILITY
00422             }
00423             break;
00424 
00425         case KSensrvPropIdMeasureRange:// 0x00000005
00426             {
00427             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_MEASURERANGE )->Des() );
00428             CleanupStack::PopAndDestroy();//R_SE_MEASURERANGE
00429             }
00430             break;
00431 
00432         case KSensrvPropIdChannelDataFormat://0x000000006
00433             {
00434             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_DATAFORMAT )->Des() );
00435             CleanupStack::PopAndDestroy();//R_SE_DATAFORMAT
00436             }
00437             break;
00438 
00439         case KSensrvPropIdScaledRange://0x000000007
00440             {
00441             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_SCALEDRANGE )->Des() );
00442             CleanupStack::PopAndDestroy();//R_SE_SCALEDRANGE
00443             }
00444             break;
00445 
00446         case KSensrvPropIdChannelAccuracy://0x000000008
00447             {
00448             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_ACCURACY )->Des() );
00449             CleanupStack::PopAndDestroy();//R_SE_ACCURACY
00450             }
00451             break;
00452 
00453         case KSensrvPropIdChannelScale://0x000000009;
00454             {
00455             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_SCALE )->Des() );
00456             CleanupStack::PopAndDestroy();//R_SE_SCALE
00457             }
00458             break;
00459 
00460         case KSensrvPropIdChannelUnit://0x0000000010
00461             {
00462             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_UNIT )->Des() );
00463             CleanupStack::PopAndDestroy();//R_SE_UNIT
00464             }
00465             break;
00466 
00467         case KSensrvSensorModel://0x0000000011
00468             {
00469             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_MODEL )->Des() );
00470             CleanupStack::PopAndDestroy();//R_SE_MODEL
00471             }
00472             break;
00473 
00474         case KSensrvSensorConnectionType://0x0000000012
00475             {
00476             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_CONNECTIONTYPE )->Des() );
00477             CleanupStack::PopAndDestroy();//R_SE_CONNECTIONTYPE
00478             }
00479             break;
00480 
00481         case KSensrvSensorDescription://0x0000000013
00482             {
00483             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_DESCRIPTION )->Des() );
00484             CleanupStack::PopAndDestroy();//R_SE_DESCRIPTION
00485             }
00486             break;
00487 
00488         case KSensrvPropIdAxisActive://0x00010001
00489             {
00490             if( TSensrvAccelerometerAxisData::EAxisX == aItemIndex )
00491                 {
00492                 aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_XAXISACTIVE )->Des() );
00493                 CleanupStack::PopAndDestroy();//R_SE_XAXISACTIVE
00494                 }
00495             else if( TSensrvAccelerometerAxisData::EAxisY == aItemIndex )
00496                 {
00497                 aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_YAXISACTIVE )->Des() );
00498                 CleanupStack::PopAndDestroy();//R_SE_YAXISACTIVE
00499                 }
00500             else if( TSensrvAccelerometerAxisData::EAxisZ == aItemIndex )
00501                 {
00502                 aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_ZAXISACTIVE )->Des() );
00503                 CleanupStack::PopAndDestroy();//R_SE_ZAXISACTIVE
00504                 }
00505             }
00506             break;
00507 
00508         case KSensrvPropIdDblTapLatency://0x00010004
00509             {
00510             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_DOUBLETAPLATENCY )->Des() );
00511             CleanupStack::PopAndDestroy();//R_SE_DOUBLETAPLATENCY
00512             }
00513             break;
00514 
00515         case KSensrvPropIdDblTapInterval://0x00010005
00516             {
00517             aPropertyBuf.Append( iCoeEnv->AllocReadResourceLC( R_SE_DOUBLETAPINTERVAL )->Des() );
00518             CleanupStack::PopAndDestroy();//R_SE_DOUBLETAPINTERVAL
00519             }
00520             break;
00521 
00522         default:
00523             {
00524             //Only number is printed for the rest of the properties.
00525             aPropertyBuf.Append( KHexPrefix );
00526             aPropertyBuf.AppendNumUC( aPropertyId, EHex );
00527             }
00528             break;
00529         }
00530 
00531     }
00532 
00533 // -----------------------------------------------------------------------------
00534 // CSensorExampleAppView::SizeChanged()
00535 // Called by framework when the view size is changed.
00536 // -----------------------------------------------------------------------------
00537 //
00538 void CSensorExampleAppView::SizeChanged()
00539     {
00540     DrawNow();
00541     }
00542 
00543 // End of File

© Nokia 2009

Back to top