homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp
changeset 69 87476091b3f5
parent 63 52b0f64eeb51
child 81 7dd137878ff8
equal deleted inserted replaced
67:474929a40a0f 69:87476091b3f5
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 
       
    18 #include <HbInstance>
       
    19 
    17 
    20 #include "hsdomainmodeldatastructures.h"
    18 #include "hsdomainmodeldatastructures.h"
    21 #include "hspagenewwidgetlayout.h"
    19 #include "hspagenewwidgetlayout.h"
    22 #include "hsscene.h"
    20 #include "hsscene.h"
    23 #include "hspage.h"
    21 #include "hspage.h"
    24 #include "hsdatabase.h"
    22 #include "hsdatabase.h"
    25 #include "hswidgethost.h"
    23 #include "hswidgethost.h"
       
    24 #include "hswidgethostvisual.h"
    26 #include "hswallpaper.h"
    25 #include "hswallpaper.h"
    27 #include "hswidgetpositioningonwidgetadd.h"
    26 #include "hswidgetpositioningonwidgetadd.h"
    28 #include "hswidgetpositioningonorientationchange.h"
    27 #include "hswidgetpositioningonorientationchange.h"
    29 #include "hsconfiguration.h"
    28 #include "hsconfiguration.h"
       
    29 #include "hsgui.h"
    30 
    30 
    31 
    31 
    32 /*!
    32 /*!
    33     \class HsPageNewWidgetLayout
    33     \class HsPageNewWidgetLayout
    34     \ingroup group_hsdomainmodel
    34     \ingroup group_hsdomainmodel
    44 HsPageNewWidgetLayout::HsPageNewWidgetLayout(const QPointF &touchPoint,
    44 HsPageNewWidgetLayout::HsPageNewWidgetLayout(const QPointF &touchPoint,
    45                                              QGraphicsLayoutItem *parent)
    45                                              QGraphicsLayoutItem *parent)
    46     : QGraphicsLayout(parent),    
    46     : QGraphicsLayout(parent),    
    47     mTouchPoint(touchPoint)
    47     mTouchPoint(touchPoint)
    48 {
    48 {
    49     mSize = HsScene::mainWindow()->layoutRect().size();
    49     mSize = HsGui::instance()->layoutRect().size();
    50 }
    50 }
    51 
    51 
    52 /*!
    52 /*!
    53     Destructor.
    53     Destructor.
    54 */
    54 */
    67 /*!
    67 /*!
    68     Returns item index of \a i.
    68     Returns item index of \a i.
    69 */
    69 */
    70 QGraphicsLayoutItem *HsPageNewWidgetLayout::itemAt(int i) const
    70 QGraphicsLayoutItem *HsPageNewWidgetLayout::itemAt(int i) const
    71 {
    71 {
    72     return mNewWidgets.at(i);
    72     return mNewWidgets.at(i)->visual();
    73 }
    73 }
    74 
    74 
    75 /*!
    75 /*!
    76     Removes item \a index.
    76     Removes item \a index.
    77 */
    77 */
   101     Recalculates children positions.
   101     Recalculates children positions.
   102 */
   102 */
   103 void HsPageNewWidgetLayout::setGeometry(const QRectF &rect)
   103 void HsPageNewWidgetLayout::setGeometry(const QRectF &rect)
   104 {
   104 {
   105     QGraphicsLayout::setGeometry(rect);
   105     QGraphicsLayout::setGeometry(rect);
   106    
   106 
   107     QList<QRectF> rects;
   107 #ifdef HSWIDGETORGANIZER_ALGORITHM
       
   108     // sort new widgets in order
       
   109     if (mNewWidgets.count() > 1) {
       
   110         // TODO: read from configuration? or just use height for portrait and width for landscape (currently only height is used)
       
   111         sortOrder order(height);
       
   112         mNewWidgets = sortWidgets(order);
       
   113     }
       
   114 #endif
       
   115 
       
   116     // get rects for new widgets
       
   117     QList<QRectF> newRects;
   108     foreach (HsWidgetHost *newWidget, mNewWidgets) {
   118     foreach (HsWidgetHost *newWidget, mNewWidgets) {
   109         rects << QRectF(QPointF(), newWidget->preferredSize());
   119         newRects << QRectF(QPointF(), newWidget->visual()->preferredSize());
   110     }
   120     }
   111 
   121 
   112     /* if there is touch point defined (widget added from context menu)
   122     /* if there is touch point defined (widget added from context menu)
   113        then there is only one widget in the list
   123        then there is only one widget in the list
   114        -> set widget center point to this touch point
   124        -> set widget center point to this touch point
   115     */
   125     */
   116     if (mTouchPoint != QPointF() && mNewWidgets.count() == 1) {
   126     if (mTouchPoint != QPointF() && mNewWidgets.count() == 1) {
   117         QRectF widgetRect = rects.at(0);
   127         QRectF widgetRect = newRects.at(0);
   118         widgetRect.moveCenter(mTouchPoint);
   128         widgetRect.moveCenter(mTouchPoint);
   119         widgetRect.moveTopLeft(HsScene::instance()->activePage()->adjustedWidgetPosition(widgetRect));
   129         widgetRect.moveTopLeft(HsScene::instance()->activePage()->adjustedWidgetPosition(widgetRect));
   120         mNewWidgets.at(0)->setGeometry(widgetRect);
   130         mNewWidgets.at(0)->visual()->setGeometry(widgetRect);
   121         /* we have to save widget presentation data here after drawing
   131         /* we have to save widget presentation data here after drawing
   122            to get correct position for later use
   132            to get correct position for later use
   123         */
   133         */
   124         mNewWidgets.at(0)->savePresentation();
   134         mNewWidgets.at(0)->savePresentation();
   125     }
   135     }
   126     // otherwise calculate position with algorithm
   136     // otherwise calculate widget positions with algorithm
   127     else {
   137     else {
       
   138         // get page rect
       
   139         QRectF pageRect = HsScene::instance()->activePage()->contentGeometry();
       
   140 
       
   141         // scan existing widgets rects
       
   142         QList<HsWidgetHost*> existingWidgets;
       
   143         existingWidgets = HsScene::instance()->activePage()->widgets();
       
   144         QList<QRectF> existingRects;
       
   145         foreach (HsWidgetHost *widget, existingWidgets) {
       
   146             if (!mNewWidgets.contains(widget)) {
       
   147                 existingRects << QRectF(widget->visual()->pos(), widget->visual()->preferredSize());
       
   148             }
       
   149         }
       
   150          
       
   151         // calculate new widget positions with "stuck 'em all"-algorithm
   128         HsWidgetPositioningOnWidgetAdd *algorithm =
   152         HsWidgetPositioningOnWidgetAdd *algorithm =
   129             HsWidgetPositioningOnWidgetAdd::instance();
   153             HsWidgetPositioningOnWidgetAdd::instance();
   130         QRectF pageRect = HsScene::instance()->activePage()->contentGeometry();
       
   131         QList<QRectF> calculatedRects =
   154         QList<QRectF> calculatedRects =
   132             algorithm->convert(pageRect, rects, QPointF());
   155             algorithm->convert(pageRect, existingRects, newRects, QPointF());
   133 
   156 
   134         for ( int i=0; i<mNewWidgets.count(); i++) {
   157         for ( int i=0; i<mNewWidgets.count(); i++) {
   135             mNewWidgets.at(i)->setGeometry(calculatedRects.at(i));
   158             mNewWidgets.at(i)->visual()->setGeometry(calculatedRects.at(i));
   136             mNewWidgets.at(i)->savePresentation();
   159             mNewWidgets.at(i)->savePresentation();
   137         }
   160         }
   138     }
   161     }
   139 }
   162 }
   140 
   163 
   143 */
   166 */
   144 void HsPageNewWidgetLayout::addItem(HsWidgetHost *item)
   167 void HsPageNewWidgetLayout::addItem(HsWidgetHost *item)
   145 {
   168 {
   146     mNewWidgets.append(item);
   169     mNewWidgets.append(item);
   147 }
   170 }
       
   171 
       
   172 #ifdef HSWIDGETORGANIZER_ALGORITHM
       
   173 // TODO: sorting should be done in algorithm class, make widget<->rect mapping here and move sortWidgets function to algorithm side
       
   174 /*!
       
   175     Sorts widgets in height/width order
       
   176 */
       
   177 QList<HsWidgetHost*> HsPageNewWidgetLayout::sortWidgets(sortOrder order)
       
   178 {
       
   179     QList<HsWidgetHost*> tmpWidgets;
       
   180 
       
   181     for ( int i = 0; i < mNewWidgets.count(); i++) {
       
   182         int index = 0;
       
   183         // add first widget to sorted list
       
   184         if (i == 0) {
       
   185             tmpWidgets << mNewWidgets.at(i);
       
   186         } else {
       
   187             // go through existing widgets in the sorted list
       
   188             for ( int j = 0; j < tmpWidgets.count(); j++) {
       
   189                 // sort widgets in height order
       
   190                 if (order == height) {
       
   191                     /* if widgets heigth is smaller on already
       
   192                        existing ones in the list -> increment index
       
   193                     */
       
   194                     if (mNewWidgets.at(i)->visual()->preferredHeight() <= tmpWidgets.at(j)->visual()->preferredHeight()) {
       
   195                         index++;
       
   196                     }
       
   197                 // sort widgets in width order
       
   198                 } else {
       
   199                     /* if widgets width is smaller on already
       
   200                        existing ones in the sorted list -> increment index
       
   201                     */
       
   202                     if (mNewWidgets.at(i)->visual()->preferredWidth() <= tmpWidgets.at(j)->visual()->preferredWidth()) {
       
   203                         index++;
       
   204                     }
       
   205                 }
       
   206             }
       
   207             // add widget to its correct index in sorted list
       
   208             tmpWidgets.insert(index, mNewWidgets.at(i));
       
   209         }
       
   210     }
       
   211     return tmpWidgets;
       
   212 }
       
   213 #endif // HSWIDGETORGANIZER_ALGORITHM