homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp
changeset 62 341166945d65
child 63 52b0f64eeb51
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
       
     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 <HbInstance>
       
    19 
       
    20 #include "hsdomainmodeldatastructures.h"
       
    21 #include "hspagenewwidgetlayout.h"
       
    22 #include "hsscene.h"
       
    23 #include "hsdatabase.h"
       
    24 #include "hswidgethost.h"
       
    25 #include "hswallpaper.h"
       
    26 #include "hswidgetpositioningonwidgetadd.h"
       
    27 #include "hswidgetpositioningonorientationchange.h"
       
    28 #include "hsconfiguration.h"
       
    29 
       
    30 
       
    31 /*!
       
    32     \class HsPageNewWidgetLayout
       
    33     \ingroup group_hsdomainmodel
       
    34     \brief Represents a page in the framework.
       
    35     HsPage is a parent for a group of widgets. HsPage can have a wallpaper.
       
    36 */
       
    37 
       
    38 /*!
       
    39     Constructor.
       
    40 
       
    41     \a parent Owner.
       
    42 */
       
    43 HsPageNewWidgetLayout::HsPageNewWidgetLayout(const QPointF &touchPoint,
       
    44                                              QGraphicsLayoutItem *parent)
       
    45     : QGraphicsLayout(parent),    
       
    46     mTouchPoint(touchPoint)
       
    47 {
       
    48     mSize = HsScene::mainWindow()->layoutRect().size();
       
    49 }
       
    50 
       
    51 /*!
       
    52     Destructor.
       
    53 */
       
    54 HsPageNewWidgetLayout::~HsPageNewWidgetLayout()
       
    55 {
       
    56 }
       
    57 
       
    58 /*!
       
    59     Returns children count.
       
    60 */
       
    61 int HsPageNewWidgetLayout::count() const
       
    62 {
       
    63     return mNewWidgets.count();
       
    64 }
       
    65 
       
    66 /*!
       
    67     Returns item index of \a i.
       
    68 */
       
    69 QGraphicsLayoutItem *HsPageNewWidgetLayout::itemAt(int i) const
       
    70 {
       
    71     return mNewWidgets.at(i);
       
    72 }
       
    73 
       
    74 /*!
       
    75     Removes item \a index.
       
    76 */
       
    77 void HsPageNewWidgetLayout::removeAt(int index)
       
    78 {
       
    79     mNewWidgets.removeAt(index);
       
    80 }
       
    81 
       
    82 /*!
       
    83     Size hint.
       
    84 */
       
    85 QSizeF HsPageNewWidgetLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
    86 {
       
    87      switch (which) {
       
    88      case Qt::MinimumSize:
       
    89      case Qt::PreferredSize:
       
    90          return mSize;
       
    91      case Qt::MaximumSize:
       
    92          return mSize;
       
    93      default:
       
    94          break;
       
    95      }
       
    96      return constraint;
       
    97 }
       
    98 
       
    99 /*!
       
   100     Recalculates children positions.
       
   101 */
       
   102 void HsPageNewWidgetLayout::setGeometry(const QRectF &rect)
       
   103 {
       
   104     QGraphicsLayout::setGeometry(rect);
       
   105    
       
   106     QList<QRectF> rects;
       
   107     foreach (HsWidgetHost *newWidget, mNewWidgets) {
       
   108         rects << QRectF(QPointF(), newWidget->preferredSize());
       
   109     }
       
   110 
       
   111     /* if there is touch point defined (widget added from context menu)
       
   112        then there is only one widget in the list
       
   113        -> set widget center point to this touch point
       
   114     */
       
   115     if (mTouchPoint != QPointF() && mNewWidgets.count() == 1) {
       
   116         QRectF pageRect = HsScene::mainWindow()->layoutRect();
       
   117         qreal widgetX = qBound(qreal(0), mTouchPoint.x() - rects.at(0).width() / 2, pageRect.width() - rects.at(0).width());
       
   118         qreal widgetY = qBound(qreal(64), mTouchPoint.y() - rects.at(0).height() / 2, pageRect.height() - rects.at(0).height());
       
   119         mNewWidgets.at(0)->setGeometry(widgetX,
       
   120                                        widgetY,
       
   121                                        rects.at(0).width(), 
       
   122                                        rects.at(0).height());
       
   123         /* we have to save widget presentation data here after drawing
       
   124            to get correct position for later use
       
   125         */
       
   126         mNewWidgets.at(0)->savePresentation();
       
   127     }
       
   128     // otherwise calculate position with algorithm
       
   129     else {
       
   130         HsWidgetPositioningOnWidgetAdd *algorithm =
       
   131             HsWidgetPositioningOnWidgetAdd::instance();
       
   132         QRectF pageRect = HsScene::mainWindow()->layoutRect();
       
   133         pageRect.adjust( (qreal)0,(qreal)64,(qreal)0,(qreal)0);
       
   134         QList<QRectF> calculatedRects =
       
   135             algorithm->convert(pageRect, rects, QPointF());
       
   136 
       
   137         for ( int i=0; i<mNewWidgets.count(); i++) {
       
   138             mNewWidgets.at(i)->setGeometry(calculatedRects.at(i));
       
   139             mNewWidgets.at(i)->savePresentation();
       
   140         }
       
   141     }
       
   142 }
       
   143 
       
   144 /*!
       
   145     Adds item to layout.
       
   146 */
       
   147 void HsPageNewWidgetLayout::addItem(HsWidgetHost *item)
       
   148 {
       
   149     mNewWidgets.append(item);
       
   150 }