widgetmodel/alfwidgetmodel/src/alfflowlayoutpreferencesimpl.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     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:  layoutpreferences implementation for flowlayoutmanager.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <assert.h>
       
    20 #include <alf/ialfwidgetcontrol.h>
       
    21 #include "alf/alfwidget.h"
       
    22 #include <alf/alfexceptions.h>
       
    23 #include <alf/ialflayoutpreferences.h>
       
    24 #include <alf/alfflowlayoutmanager.h>
       
    25 #include <alf/ialfflowlayoutpolicy.h>
       
    26 #include <osn/osnnew.h>
       
    27 
       
    28 //stl
       
    29 #include <algorithm> //for min & max
       
    30 #include "alfflowlayoutpreferencesimpl.h"
       
    31 
       
    32 using namespace Alf;
       
    33 
       
    34 namespace Alf
       
    35     {
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 AlfFlowLayoutPreferencesImpl::AlfFlowLayoutPreferencesImpl(
       
    39     AlfFlowLayoutManager& aFlowLayoutManager) : 
       
    40     mFlowLayoutManager(aFlowLayoutManager), mPreferredSize(TAlfMetric(0))
       
    41     {
       
    42     }
       
    43 // ---------------------------------------------------------------------------
       
    44 // from IAlfLayoutPreferences
       
    45 // Returns the minimum area that this layout manager can occupy by observing
       
    46 // the minimum sizes of the child UI elements in the layout
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 bool AlfFlowLayoutPreferencesImpl::getMinimumSize(TAlfXYMetric& aMinSize) const
       
    50 	{
       
    51     return calculateChildSizes(aMinSize, 
       
    52         flowLayoutPolicy().flowDirection(), ESizeMin);
       
    53 	}
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // from IAlfLayoutPreferences
       
    57 // Returns the maximum area that this layout manager can occupy by observing
       
    58 // the maximum sizes of the child UI elements in the layout
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 bool AlfFlowLayoutPreferencesImpl::getMaximumSize(TAlfXYMetric& aMaxSize) const
       
    62 	{
       
    63     return calculateChildSizes(aMaxSize, 
       
    64         flowLayoutPolicy().flowDirection(), ESizeMax);
       
    65 	}
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // Combines and returns the preferred sizes of all child UI elements according
       
    69 // to the layouting rules.
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 bool AlfFlowLayoutPreferencesImpl::getPreferredSize(TAlfXYMetric& aPreferredSize) const
       
    73     {
       
    74     bool b = false;
       
    75     bool manualSet = (mPreferredSize.iX.iMagnitude > 0 && 
       
    76                       mPreferredSize.iY.iMagnitude > 0);
       
    77     
       
    78     if (manualSet)
       
    79         {
       
    80         aPreferredSize = mPreferredSize;
       
    81         b = true;
       
    82         }
       
    83     else
       
    84         {
       
    85         b = calculateChildSizes(aPreferredSize, 
       
    86             flowLayoutPolicy().flowDirection(), ESizePreferred);
       
    87         }
       
    88     return b;
       
    89 	}
       
    90  
       
    91 // ---------------------------------------------------------------------------
       
    92 // from IAlfLayoutPreferences
       
    93 // At the moment doesn't do anything since preferred size is being queried
       
    94 // from the child UI elements added to this layout manager.
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void AlfFlowLayoutPreferencesImpl::setPreferredSize( 
       
    98     const TAlfXYMetric& aPreferredSize )
       
    99 	{
       
   100 	mPreferredSize = aPreferredSize;
       
   101     mFlowLayoutManager.owner().updateParentLayout();
       
   102 	}
       
   103     
       
   104 // ---------------------------------------------------------------------------
       
   105 // From class IAlfInterfaceBase.
       
   106 // Getter for interfaces provided by the element.
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 IAlfInterfaceBase* AlfFlowLayoutPreferencesImpl::makeInterface(
       
   110     const IfId& aType)
       
   111     {
       
   112     UString param(aType.mImplementationId);
       
   113     IAlfInterfaceBase* ret = 0;
       
   114     if (param == IAlfLayoutPreferences::type().mImplementationId)
       
   115         {
       
   116         ret = static_cast<IAlfLayoutPreferences*>(this);
       
   117         }
       
   118     
       
   119     return ret;    
       
   120     }
       
   121     
       
   122 // ---------------------------------------------------------------------------
       
   123 // calculate the min-, preferred-, or max- sizes from the children. 
       
   124 // ---------------------------------------------------------------------------
       
   125 //    
       
   126 bool AlfFlowLayoutPreferencesImpl::calculateChildSizes(
       
   127     TAlfXYMetric& aSize,  
       
   128     enum IAlfFlowLayoutPolicy::flowDirection aFlowDirection, 
       
   129     enum AlfFlowLayoutPreferencesImpl::sizeType aSizeType) const
       
   130     {
       
   131 	bool ret = false; 
       
   132 	TAlfXYMetric resultSize(TAlfMetric(0), TAlfMetric(0));
       
   133 	
       
   134     // Iterate through all the child controls, and get their
       
   135     // preferred sizes.
       
   136     int childCount = mFlowLayoutManager.count();
       
   137     for (int i = 0; i < childCount; i++)
       
   138         {
       
   139         CAlfWidgetControl* childWidgetControl = mFlowLayoutManager.getControl(i);
       
   140         
       
   141         TAlfXYMetric size;
       
   142         if (getSize(childWidgetControl, size, aSizeType))
       
   143             {
       
   144             TAlfRealSize pixelSize = sizeInPixels(size);
       
   145             
       
   146             if (aFlowDirection == IAlfFlowLayoutPolicy::EFlowVertical)
       
   147                 {
       
   148                 //total width = max preferred width of child widgets.
       
   149                 //total height = sum of the preferred heights of child widgets.
       
   150             	if(pixelSize.iWidth > resultSize.iX.iMagnitude)
       
   151             		{
       
   152             		resultSize.iX.iMagnitude = pixelSize.iWidth;
       
   153             		}
       
   154         		resultSize.iY.iMagnitude += pixelSize.iHeight;
       
   155         		ret = true;
       
   156                 }
       
   157             else
       
   158                 {
       
   159                 //horizontal flow direction
       
   160                 //total height = max preferred height of child widgets.
       
   161                 //total width = sum of the preferred widths of child widgets.
       
   162             	if(pixelSize.iHeight > resultSize.iY.iMagnitude)
       
   163             		{
       
   164             		resultSize.iY.iMagnitude = pixelSize.iHeight;
       
   165             		}
       
   166         		resultSize.iX.iMagnitude += pixelSize.iWidth;
       
   167         		ret = true;
       
   168                 }
       
   169             }
       
   170         }
       
   171 	
       
   172     aSize = resultSize;
       
   173 	return ret;
       
   174     }
       
   175     
       
   176 // ---------------------------------------------------------------------------
       
   177 // fetches the size from the IAlfLayoutPreferences-interface of the aControl.
       
   178 // ---------------------------------------------------------------------------
       
   179 //    
       
   180 bool AlfFlowLayoutPreferencesImpl::getSize(
       
   181     CAlfWidgetControl* aControl,
       
   182     TAlfXYMetric& aSize,
       
   183     enum AlfFlowLayoutPreferencesImpl::sizeType aSizeType) const
       
   184     {
       
   185     bool ret = false;
       
   186     
       
   187     if (aControl)
       
   188         {
       
   189         const IAlfLayoutPreferences* layoutPrefs = 
       
   190             mFlowLayoutManager.getLayoutPreferences(aControl);
       
   191         
       
   192         if (layoutPrefs)
       
   193             {
       
   194             switch(aSizeType)
       
   195                 {
       
   196                 case AlfFlowLayoutPreferencesImpl::ESizePreferred:
       
   197                     ret = layoutPrefs->getPreferredSize(aSize);
       
   198                     break;
       
   199                 case AlfFlowLayoutPreferencesImpl::ESizeMin:
       
   200                     ret = layoutPrefs->getMinimumSize(aSize);
       
   201                     break;
       
   202                 case AlfFlowLayoutPreferencesImpl::ESizeMax:
       
   203                     ret = layoutPrefs->getMaximumSize(aSize);
       
   204                     break;
       
   205                 }
       
   206             }
       
   207         }
       
   208         
       
   209     return ret;
       
   210     }
       
   211     
       
   212 // ---------------------------------------------------------------------------
       
   213 // get the flowlayoutpolicy-interface.
       
   214 // ---------------------------------------------------------------------------
       
   215 //    
       
   216 const IAlfFlowLayoutPolicy& AlfFlowLayoutPreferencesImpl::flowLayoutPolicy() const
       
   217     {
       
   218 	AlfFlowLayoutManager* man = 
       
   219 	    const_cast<AlfFlowLayoutManager*>(&mFlowLayoutManager);
       
   220 	IAlfFlowLayoutPolicy* policy = 
       
   221 	    IAlfInterfaceBase::makeInterface<IAlfFlowLayoutPolicy>(man);
       
   222     assert(policy);
       
   223     return *policy;
       
   224     }
       
   225 // ---------------------------------------------------------------------------
       
   226 // converts size in metrics to size in pixels.
       
   227 // implementation missing!
       
   228 // ---------------------------------------------------------------------------
       
   229 //    
       
   230 TAlfRealSize AlfFlowLayoutPreferencesImpl::sizeInPixels(
       
   231     const TAlfXYMetric& aSize)
       
   232     {
       
   233     TAlfRealSize s;
       
   234 
       
   235     if(aSize.iX.iUnit == EAlfUnitPixel)
       
   236     	{
       
   237     	s.iWidth = aSize.iX.iMagnitude;
       
   238     	}
       
   239     if(aSize.iY.iUnit == EAlfUnitPixel)
       
   240         {
       
   241         s.iHeight = aSize.iY.iMagnitude;
       
   242         }
       
   243 
       
   244     return s;
       
   245     }
       
   246 } // Alf