homescreenapp/hsutils/src/hswidgetpositioningonorientationchange.cpp
branchRCL_3
changeset 82 5f0182e07bfb
equal deleted inserted replaced
79:f00a6757af32 82:5f0182e07bfb
       
     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 "hswidgetpositioningonorientationchange.h"
       
    20 
       
    21 
       
    22 
       
    23 /*!
       
    24     \class HsWidgetPositioningOnOrientationChange
       
    25     \ingroup group_hsutils
       
    26     \brief Defines widget positioning on orientation change.
       
    27 
       
    28     Widget positioning on orientation change sets positions for
       
    29     a set of home screen widgets from landscape to portrait
       
    30     orientation and vice versa.
       
    31 */
       
    32 
       
    33 /*!
       
    34     Sets the positioning \a instance as the current one.
       
    35     Deletes the existing instance if present.
       
    36 */
       
    37 void HsWidgetPositioningOnOrientationChange::setInstance(
       
    38     HsWidgetPositioningOnOrientationChange *instance)
       
    39 {
       
    40     if (mInstance)
       
    41         delete mInstance;
       
    42     mInstance = instance;
       
    43 }
       
    44 
       
    45 /*!
       
    46     Returns the current positioning instance.
       
    47 */
       
    48 HsWidgetPositioningOnOrientationChange *HsWidgetPositioningOnOrientationChange::instance()
       
    49 {
       
    50     return mInstance;
       
    51 }
       
    52 
       
    53 /*!
       
    54     Stores the current positioning instance.
       
    55 */
       
    56 HsWidgetPositioningOnOrientationChange *HsWidgetPositioningOnOrientationChange::mInstance = 0;
       
    57 
       
    58 /*!
       
    59     \class HsSimpleWidgetPositioningOnOrientationChange
       
    60     \brief Bounds center points of input geometries to
       
    61            the given output rectangle.
       
    62 */
       
    63 
       
    64 /*!
       
    65     \class HsAdvancedWidgetPositioningOnOrientationChange
       
    66     \brief More advanced widget positioning algorithm.
       
    67 
       
    68     Calculates new center points of
       
    69     \a fromGeometries when moving from \a fromRect to \a toRect.
       
    70     Screen is divided to two pieces:
       
    71     \verbatim
       
    72     ------
       
    73     | A  |
       
    74     |    |
       
    75     ------
       
    76     | B  |
       
    77     |    |
       
    78     ------
       
    79     \endverbatim
       
    80     and after conversion widgets from A are moved to A' and widgets from B to B':
       
    81     \verbatim
       
    82     |-----|-----|
       
    83     |A'   | B'  |
       
    84     |-----|-----|
       
    85     \endverbatim
       
    86     and vice versa.
       
    87 */
       
    88 QList<QRectF> HsAdvancedWidgetPositioningOnOrientationChange::convert(
       
    89     const QRectF &fromRect,
       
    90     const QList<QRectF> &fromGeometries,
       
    91     const QRectF &toRect)
       
    92 {
       
    93     QList<QRectF> toGeometries;
       
    94 
       
    95     // Portrait -> Landscape
       
    96     if (fromRect.width() < fromRect.height()) {
       
    97         foreach (QRectF g, fromGeometries) {
       
    98             
       
    99             QRectF tg(g.topLeft() - fromRect.topLeft(), g.size());
       
   100             
       
   101             qreal x = tg.center().x() / fromRect.width();
       
   102             qreal y = tg.center().y() / (fromRect.height() / 2);
       
   103 
       
   104             if (y < 1) {
       
   105                 x *= toRect.width() / 2;
       
   106                 y *= toRect.height();
       
   107             } else {
       
   108                 x *= toRect.width() / 2;
       
   109                 x += toRect.width() / 2;
       
   110                 y -= 1;
       
   111                 y *= toRect.height();
       
   112             }
       
   113             
       
   114             x = qBound(g.width() / 2, x, toRect.width() - g.width() / 2);
       
   115             y = qBound(g.height() / 2, y, toRect.height() - g.height() / 2);
       
   116 
       
   117             g.moveCenter(QPointF(x, y) + toRect.topLeft());
       
   118 
       
   119             toGeometries.append(g);
       
   120         }
       
   121     } else { // Landscape -> Portrait
       
   122         foreach (QRectF g, fromGeometries) {
       
   123             
       
   124             QRectF tg(g.topLeft() - fromRect.topLeft(), g.size());
       
   125             
       
   126             qreal x = tg.center().x() / (fromRect.width() / 2);
       
   127             qreal y = tg.center().y() / fromRect.height();
       
   128 
       
   129             if (x < 1) {
       
   130                 x *= toRect.width();
       
   131                 y *= toRect.height() / 2;
       
   132             } else {
       
   133                 x -= 1;
       
   134                 x += toRect.width();
       
   135                 y *= toRect.height() / 2;
       
   136                 y += toRect.height() / 2;
       
   137             }
       
   138             
       
   139             x = qBound(g.width() / 2, x, toRect.width() - g.width() / 2);
       
   140             y = qBound(g.height() / 2, y, toRect.height() - g.height() / 2);
       
   141 
       
   142             g.moveCenter(QPointF(x, y) + toRect.topLeft());
       
   143 
       
   144             toGeometries.append(g);
       
   145         }
       
   146     }
       
   147 
       
   148     return toGeometries;
       
   149 }