camerauis/cameraapp/generic/src/camlinevfgriddrawer.cpp
branchRCL_3
changeset 24 bac7acad7cb3
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
       
     1 /*
       
     2 * Copyright (c) 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:  Implemantation for CCamLineVfGridDrawer class.
       
    15 *
       
    16 *  Copyright © 2007 Nokia.  All rights reserved.
       
    17 *  This material, including documentation and any related computer
       
    18 *  programs, is protected by copyright controlled by Nokia.  All
       
    19 *  rights are reserved.  Copying, including reproducing, storing,
       
    20 *  adapting or translating, any or all of this material requires the
       
    21 *  prior written consent of Nokia.  This material also contains
       
    22 *  confidential information which may not be disclosed to others
       
    23 *  without the prior written consent of Nokia.
       
    24 
       
    25 *
       
    26 *
       
    27 */
       
    28 
       
    29 
       
    30 #include <gdi.h>
       
    31 #include "camlinevfgriddrawer.h"
       
    32 #include "camlogging.h"
       
    33 
       
    34 // ======== LOCAL CONSTANTS ========
       
    35 
       
    36 namespace
       
    37   {
       
    38   const TInt KArrayGranularity =   8;
       
    39   const TInt KMaxLineCount     =  64;
       
    40 
       
    41   const TInt KPenColorRed   = 255;
       
    42   const TInt KPenColorGreen =   0;//255;
       
    43   const TInt KPenColorBlue  =   0;//255;
       
    44   const TInt KPenColorAlpha = 255;//128;
       
    45   const TInt KPenWidth      =   2;
       
    46   const TInt KPenHeight     =   2;
       
    47   const CGraphicsContext::TPenStyle KPenStyle = CGraphicsContext::ESolidPen;  
       
    48   }
       
    49   
       
    50 // ======== LOCAL FUNCTIONS ========
       
    51 
       
    52 // ======== MEMBER FUNCTIONS ========
       
    53 
       
    54 // ===========================================================================
       
    55 // Public part
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // static NewL
       
    59 //
       
    60 // 2-phased constructor
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CCamLineVfGridDrawer* 
       
    64 CCamLineVfGridDrawer::NewL()
       
    65   {
       
    66   CCamLineVfGridDrawer* self = new (ELeave) CCamLineVfGridDrawer;
       
    67   CleanupStack::PushL( self );
       
    68   self->ConstructL();
       
    69   CleanupStack::Pop( self );
       
    70   return self;
       
    71   }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Destructor
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 CCamLineVfGridDrawer::~CCamLineVfGridDrawer()
       
    78   {
       
    79   PRINT( _L("Camera => ~CCamLineVfGridDrawer") );
       
    80   iLineArray.Reset();
       
    81   iLineArray.Close();
       
    82   PRINT( _L("Camera <= ~CCamLineVfGridDrawer") );
       
    83   }
       
    84     
       
    85 
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // virtual InitL
       
    89 // * from MCamVfGridDrawer
       
    90 // 
       
    91 // Initialize the drawer.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void 
       
    95 CCamLineVfGridDrawer::InitL( TAny* /*aParam*/ )
       
    96   {
       
    97   }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // virtual Draw( CBitmapContext& )
       
   101 //   * from MCamVfGridDrawer
       
   102 // 
       
   103 // Draw the grid. If not set visible nothing is drawn.
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void 
       
   107 CCamLineVfGridDrawer::Draw( CBitmapContext& aGc ) const
       
   108   {
       
   109   if( iVisible &&   
       
   110       iLineArray.Count() > 0 )
       
   111     {
       
   112     // Set drawing properties.
       
   113     aGc.SetPenStyle( iPenStyle );
       
   114     aGc.SetPenSize ( iPenSize  );
       
   115     aGc.SetPenColor( iPenColor );
       
   116 
       
   117     // Draw the lines
       
   118     for( TInt i = 0; i < iLineArray.Count(); i++ )
       
   119       {
       
   120       // Top-left and bottom-right corners of the rect 
       
   121       // represent the line ends.
       
   122       aGc.DrawLine( iLineArray[i].iTl, iLineArray[i].iBr );
       
   123       }
       
   124 
       
   125     // Clear any settings made to gc
       
   126     aGc.Reset();
       
   127     }
       
   128   else
       
   129     {
       
   130     // Not visible => draw nothing.
       
   131     }
       
   132   }
       
   133 
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // virtual Draw( const TRect&, CBitmapContext& )
       
   137 //   * from MCamVfGridDrawer
       
   138 // 
       
   139 // Draw part of the grid. If not set visible nothing is drawn.
       
   140 // Note:
       
   141 //   Only pure vertical and pure horizontal lines
       
   142 //   get drawn to right place if lines expand outside
       
   143 //   the aRect rectangle given here as parameter.
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void 
       
   147 CCamLineVfGridDrawer::Draw( const TRect&          aRect,
       
   148                                   CBitmapContext& aGc   ) const
       
   149   {
       
   150   if( iVisible &&   
       
   151       iLineArray.Count() > 0 )
       
   152     {
       
   153     // Limit the drawing to given rectangle.
       
   154     // Drawing outside this rectangle has no visible effect.
       
   155     aGc.SetClippingRect( aRect );
       
   156     // Make the same drawing operations as in full screen draw.
       
   157     // The drawing is so simple that any optimization is
       
   158     // probably more time consuming than the drawing itself.
       
   159     // Draw resets the gc in the end, so clipping rect is cleared.
       
   160     Draw( aGc );
       
   161     }
       
   162   else
       
   163     {
       
   164     // Not visible => draw nothing.
       
   165     }
       
   166   }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // virtual SetVisible
       
   170 // * from MCamVfGridDrawer
       
   171 // 
       
   172 // Set visibility of the grid.
       
   173 // 
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void 
       
   177 CCamLineVfGridDrawer::SetVisible( TBool aVisible )
       
   178   {
       
   179   iVisible = aVisible;
       
   180   }
       
   181 
       
   182 
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // virtual IsVisible
       
   186 // * from MCamVfGridDrawer
       
   187 // 
       
   188 // Get visibility of the grid.
       
   189 // 
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 TBool
       
   193 CCamLineVfGridDrawer::IsVisible() const
       
   194   {
       
   195   return iVisible;
       
   196   }
       
   197 
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // SetLinesL
       
   201 // * from MCamVfGridDrawer
       
   202 // 
       
   203 // Set the line end pair array.
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void 
       
   207 CCamLineVfGridDrawer::SetLinesL( const RLineArray& aLineArray )
       
   208   {
       
   209   TInt newLineCount = aLineArray.Count();
       
   210   if( newLineCount > KMaxLineCount )
       
   211     {
       
   212     User::Leave( KErrArgument );
       
   213     }
       
   214   
       
   215   iLineArray.Reset();
       
   216   for( TInt i = 0; i < newLineCount; i++ )
       
   217     {
       
   218     iLineArray.Append( aLineArray[i] );
       
   219     }
       
   220   }
       
   221 
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // SetLinesL
       
   225 // 
       
   226 // Set the line end pair array by defining the count and bordered property
       
   227 // of lines per horizontal and vertical axis.
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 void 
       
   231 CCamLineVfGridDrawer::SetLinesL( const TRect& aDrawingRect, 
       
   232                                        TInt   aHorizontalLines, 
       
   233                                        TInt   aVerticalLines,
       
   234                                        TBool  aBorderedGrid /* = EFalse */)
       
   235   {
       
   236   // -------------------------------------------------------
       
   237   // Check the max limit for lines does not exceed.
       
   238   if( aHorizontalLines < 0
       
   239    || aVerticalLines   < 0
       
   240    || aHorizontalLines + aVerticalLines > KMaxLineCount
       
   241     )
       
   242     {
       
   243     User::Leave( KErrArgument );
       
   244     }
       
   245 
       
   246   // -------------------------------------------------------
       
   247   // Fill the line end array
       
   248   //
       
   249   // The lines are evenly spaced over the draw area axis in question.  
       
   250   // The aBorderedGrid parameter determines whether the grid 
       
   251   // draws lines to the borders of draw area:
       
   252   // E.g. 3 vertical lines: '[' and ']' are the draw area borders.
       
   253   //  aBorderedGrid: [|    |    |]
       
   254   // !aBorderedGrid: [  |  |  |  ]  
       
   255   //
       
   256   // The formula for the location of i:th line when N lines
       
   257   // are used along dimension D:
       
   258   // -  aBorderedGrid: (  i   * D) / (N - 1) 
       
   259   // - !aBorderedGrid: ((i+1) * D) / (N + 1) 
       
   260   // * Divide by (N-1) needs special handling with N=1.
       
   261   // * Half of pen width is subtracted from the above formula result
       
   262   //   to center the line to the calculated location.
       
   263   // * odd area or pen width/height may cause error of 1 pixel.
       
   264   TInt indexOffset = (aBorderedGrid ?  0 : 1);
       
   265   TInt countOffset = (aBorderedGrid ? -1 : 1);
       
   266 
       
   267   // -----------------------------------
       
   268   // First horizontal lines
       
   269   TInt height  = aDrawingRect.Height();
       
   270   TInt leftX   = aDrawingRect.iTl.iX;
       
   271   TInt rightX  = aDrawingRect.iBr.iX;
       
   272 
       
   273   TInt i = 0;
       
   274   while( i < aHorizontalLines )
       
   275     {
       
   276     // Max used to make sure no div-by-zero
       
   277     TInt currentY = ((i+indexOffset)*height) / Max(1,aHorizontalLines+countOffset) - KPenHeight/2;
       
   278     iLineArray.Append( TRect( leftX, currentY, rightX, currentY ) );
       
   279     i++;
       
   280     }
       
   281 
       
   282   // -----------------------------------
       
   283   // Then vertical lines
       
   284   TInt width   = aDrawingRect.Width();
       
   285   TInt topY    = aDrawingRect.iTl.iY;
       
   286   TInt bottomY = aDrawingRect.iBr.iY;
       
   287 
       
   288   TInt j = 0;
       
   289   while( j < aVerticalLines )
       
   290     {
       
   291     // Max used to make sure no div-by-zero
       
   292     TInt currentX = leftX + ((j+indexOffset)*width) / Max(1,aVerticalLines+countOffset) - KPenWidth/2;
       
   293     iLineArray.Append( TRect( currentX, topY, currentX, bottomY ) );
       
   294     j++;
       
   295     }
       
   296   // -------------------------------------------------------
       
   297   }
       
   298 
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // SetPenStyle
       
   302 // 
       
   303 // Set the style of drawing pen.
       
   304 // See CGraphicsContext.
       
   305 // ---------------------------------------------------------------------------
       
   306 //
       
   307 void 
       
   308 CCamLineVfGridDrawer::SetPenStyle( const CGraphicsContext::TPenStyle& aPenStyle )
       
   309   {
       
   310   iPenStyle = aPenStyle;
       
   311   }
       
   312 
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // SetPenSize
       
   316 // 
       
   317 // Set the size of drawing pen.
       
   318 // See CGraphicsContext.
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 void 
       
   322 CCamLineVfGridDrawer::SetPenSize( const TSize& aPenSize )
       
   323   {
       
   324   iPenSize = aPenSize;
       
   325   }
       
   326 
       
   327 // ---------------------------------------------------------------------------
       
   328 // SetPenColor
       
   329 // 
       
   330 // Set the color of drawing pen.
       
   331 // See CGraphicsContext.
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 void 
       
   335 CCamLineVfGridDrawer::SetPenColor( const TRgb aPenColor )
       
   336   {
       
   337   iPenColor = aPenColor;
       
   338   }
       
   339 
       
   340 
       
   341 // ===========================================================================
       
   342 // Protected part
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // ConstructL
       
   346 // 
       
   347 // 2nd phase constructor.
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 void 
       
   351 CCamLineVfGridDrawer::ConstructL()
       
   352   {
       
   353   // empty
       
   354   }
       
   355 
       
   356 
       
   357 
       
   358 // ===========================================================================
       
   359 // Private part
       
   360 
       
   361 // ---------------------------------------------------------------------------
       
   362 // C++ constructor.
       
   363 // Called in 1st phase of construction.
       
   364 // ---------------------------------------------------------------------------
       
   365 //
       
   366 CCamLineVfGridDrawer::CCamLineVfGridDrawer()
       
   367   : iLineArray( KArrayGranularity ),
       
   368     iVisible( EFalse ),
       
   369     iPenStyle( KPenStyle ),
       
   370     iPenSize( KPenWidth, KPenHeight ),
       
   371     iPenColor( KPenColorRed, KPenColorGreen, KPenColorBlue, KPenColorAlpha )
       
   372   {
       
   373   // empty
       
   374   }
       
   375 
       
   376 
       
   377 // ============================== end of file ================================