homescreenapp/hsutils/src/hswidgetpositioningonwidgetadd.cpp
changeset 35 f9ce957a272c
child 36 cdae8c6c3876
equal deleted inserted replaced
5:c743ef5928ba 35:f9ce957a272c
       
     1 /*
       
     2 * Copyright (c) 2009 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 #include <QtGlobal>
       
    19 #include "hswidgetpositioningonwidgetadd.h"
       
    20 #include <math.h>
       
    21 #include <QLineF>
       
    22 
       
    23 const qreal offset = 40; //TODO: Implement this as configurable parameter
       
    24 
       
    25 /*!
       
    26     \class HsWidgetPositioningOnWidgetAdd
       
    27     \brief Defines widget positioning on widget add.
       
    28 
       
    29     Widget positioning on widget add sets positions for
       
    30     a set of home screen widgets added from application library.
       
    31 */
       
    32 
       
    33 /*!
       
    34     Sets the positioning \a instance as the current one.
       
    35     Deletes the existing instance if present.
       
    36 */
       
    37 void HsWidgetPositioningOnWidgetAdd::setInstance(
       
    38     HsWidgetPositioningOnWidgetAdd *instance)
       
    39 {
       
    40     if (mInstance)
       
    41         delete mInstance;
       
    42     mInstance = instance;
       
    43 }
       
    44  
       
    45 /*!
       
    46     Returns the current positioning instance.
       
    47 */
       
    48 HsWidgetPositioningOnWidgetAdd *HsWidgetPositioningOnWidgetAdd::instance()
       
    49 {
       
    50     return mInstance;
       
    51 }
       
    52 
       
    53 /*!
       
    54     Stores the current positioning instance.
       
    55 */
       
    56 HsWidgetPositioningOnWidgetAdd *HsWidgetPositioningOnWidgetAdd::mInstance = 0;
       
    57 
       
    58 /*!
       
    59     \class HsAnchorPointInBottomRight
       
    60     \brief Diagonal widget positioning algorithm.
       
    61     
       
    62     Sets widget's lower right corner to follow content area's diagonal.
       
    63     Widgets are positioned to certain offset to each other.
       
    64 */
       
    65 QList<QRectF> HsAnchorPointInBottomRight::convert(
       
    66     const QRectF &contentArea,
       
    67     const QList<QRectF> &widgets)
       
    68 {
       
    69     QList<QRectF> toGeometries;
       
    70 
       
    71     //Offset for widgets' bottom right position to each other
       
    72     qreal k = contentArea.height()/contentArea.width(); //slope of the diagonal
       
    73     qreal offset_x = offset/(sqrt(k + 1));
       
    74     qreal offset_y = k*offset_x;
       
    75     QPointF offsetPoint(offset_x, offset_y);
       
    76     
       
    77     QLineF diagonal(contentArea.topLeft(), contentArea.bottomRight());
       
    78     QLineF widgetRight(contentArea.center().x()+ widgets.at(0).width()/2,
       
    79                        contentArea.top(),
       
    80                        contentArea.center().x()+ widgets.at(0).width()/2,
       
    81                        contentArea.bottom());
       
    82 
       
    83     QPointF anchorPoint;
       
    84     if(QLineF::BoundedIntersection != diagonal.intersect(widgetRight, &anchorPoint)) {
       
    85         return widgets; //Return original since undefined error.
       
    86                         //In this case widget's must be wider than the content area.
       
    87     }
       
    88 
       
    89     //First widget to the center of the content area
       
    90     foreach (QRectF g, widgets) {
       
    91         g.moveBottomRight(anchorPoint);
       
    92         toGeometries << g;
       
    93         anchorPoint -= offsetPoint;
       
    94         if(!contentArea.contains(anchorPoint)) {
       
    95             anchorPoint = contentArea.bottomRight();
       
    96         }
       
    97     }
       
    98     return toGeometries;
       
    99 }
       
   100 
       
   101 /*!
       
   102     \class HsAnchorPointInCenter
       
   103     \brief Diagonal widget positioning algorithm.
       
   104     
       
   105     Sets widget's center point to follow content area's diagonal.
       
   106     Widgets are positioned to certain offset to each other.
       
   107 */
       
   108 #ifdef COVERAGE_MEASUREMENT
       
   109 #pragma CTC SKIP
       
   110 #endif //COVERAGE_MEASUREMENT
       
   111 QList<QRectF> HsAnchorPointInCenter::convert(
       
   112     const QRectF &contentArea,
       
   113     const QList<QRectF> &widgets)
       
   114 {
       
   115     QList<QRectF> toGeometries;
       
   116 
       
   117     //Offset for widgets' centers position to each other
       
   118     qreal k = contentArea.height()/contentArea.width(); //slope of the diagonal
       
   119     qreal offset_x = offset/(sqrt(k + 1));
       
   120     qreal offset_y = k*offset_x;
       
   121     QPointF offsetPoint(offset_x, offset_y);
       
   122 
       
   123     //First widget to the center of the content area
       
   124     QPointF anchorPoint = contentArea.center();
       
   125     foreach (QRectF g, widgets) {
       
   126         g.moveCenter(anchorPoint);
       
   127         toGeometries << g;
       
   128         anchorPoint -= offsetPoint;
       
   129         if(!contentArea.contains(anchorPoint)) {
       
   130             anchorPoint = contentArea.bottomRight();
       
   131         }
       
   132     }
       
   133     return toGeometries;
       
   134 }
       
   135 
       
   136 #ifdef COVERAGE_MEASUREMENT
       
   137 #pragma CTC ENDSKIP
       
   138 #endif //COVERAGE_MEASUREMENT
       
   139