homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp
changeset 77 4b195f3bea29
parent 61 2b1b11a301d2
child 85 35368b604b28
--- a/homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp	Tue Jul 06 14:06:53 2010 +0300
+++ b/homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp	Wed Aug 18 09:40:07 2010 +0300
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
@@ -15,17 +15,18 @@
 *
 */
 
-#include <HbInstance>
-
 #include "hsdomainmodeldatastructures.h"
 #include "hspagenewwidgetlayout.h"
 #include "hsscene.h"
+#include "hspage.h"
 #include "hsdatabase.h"
 #include "hswidgethost.h"
+#include "hswidgethostvisual.h"
 #include "hswallpaper.h"
 #include "hswidgetpositioningonwidgetadd.h"
 #include "hswidgetpositioningonorientationchange.h"
 #include "hsconfiguration.h"
+#include "hsgui.h"
 
 
 /*!
@@ -45,7 +46,7 @@
     : QGraphicsLayout(parent),    
     mTouchPoint(touchPoint)
 {
-    mSize = HsScene::mainWindow()->layoutRect().size();
+    mSize = HsGui::instance()->layoutRect().size();
 }
 
 /*!
@@ -68,7 +69,7 @@
 */
 QGraphicsLayoutItem *HsPageNewWidgetLayout::itemAt(int i) const
 {
-    return mNewWidgets.at(i);
+    return mNewWidgets.at(i)->visual();
 }
 
 /*!
@@ -102,10 +103,20 @@
 void HsPageNewWidgetLayout::setGeometry(const QRectF &rect)
 {
     QGraphicsLayout::setGeometry(rect);
-   
-    QList<QRectF> rects;
+
+#ifdef HSWIDGETORGANIZER_ALGORITHM
+    // sort new widgets in order
+    if (mNewWidgets.count() > 1) {
+        // TODO: read from configuration? or just use height for portrait and width for landscape (currently only height is used)
+        sortOrder order(height);
+        mNewWidgets = sortWidgets(order);
+    }
+#endif
+
+    // get rects for new widgets
+    QList<QRectF> newRects;
     foreach (HsWidgetHost *newWidget, mNewWidgets) {
-        rects << QRectF(QPointF(), newWidget->preferredSize());
+        newRects << QRectF(QPointF(), newWidget->visual()->preferredSize());
     }
 
     /* if there is touch point defined (widget added from context menu)
@@ -113,29 +124,38 @@
        -> set widget center point to this touch point
     */
     if (mTouchPoint != QPointF() && mNewWidgets.count() == 1) {
-        QRectF pageRect = HsScene::mainWindow()->layoutRect();
-        qreal widgetX = qBound(qreal(0), mTouchPoint.x() - rects.at(0).width() / 2, pageRect.width() - rects.at(0).width());
-        qreal widgetY = qBound(qreal(64), mTouchPoint.y() - rects.at(0).height() / 2, pageRect.height() - rects.at(0).height());
-        mNewWidgets.at(0)->setGeometry(widgetX,
-                                       widgetY,
-                                       rects.at(0).width(), 
-                                       rects.at(0).height());
+        QRectF widgetRect = newRects.at(0);
+        widgetRect.moveCenter(mTouchPoint);
+        widgetRect.moveTopLeft(HsScene::instance()->activePage()->adjustedWidgetPosition(widgetRect));
+        mNewWidgets.at(0)->visual()->setGeometry(widgetRect);
         /* we have to save widget presentation data here after drawing
            to get correct position for later use
         */
         mNewWidgets.at(0)->savePresentation();
     }
-    // otherwise calculate position with algorithm
+    // otherwise calculate widget positions with algorithm
     else {
+        // get page rect
+        QRectF pageRect = HsScene::instance()->activePage()->contentGeometry();
+
+        // scan existing widgets rects
+        QList<HsWidgetHost*> existingWidgets;
+        existingWidgets = HsScene::instance()->activePage()->widgets();
+        QList<QRectF> existingRects;
+        foreach (HsWidgetHost *widget, existingWidgets) {
+            if (!mNewWidgets.contains(widget)) {
+                existingRects << QRectF(widget->visual()->pos(), widget->visual()->preferredSize());
+            }
+        }
+         
+        // calculate new widget positions with "stuck 'em all"-algorithm
         HsWidgetPositioningOnWidgetAdd *algorithm =
             HsWidgetPositioningOnWidgetAdd::instance();
-        QRectF pageRect = HsScene::mainWindow()->layoutRect();
-        pageRect.adjust( (qreal)0,(qreal)64,(qreal)0,(qreal)0);
         QList<QRectF> calculatedRects =
-            algorithm->convert(pageRect, rects, QPointF());
+            algorithm->convert(pageRect, existingRects, newRects, QPointF());
 
         for ( int i=0; i<mNewWidgets.count(); i++) {
-            mNewWidgets.at(i)->setGeometry(calculatedRects.at(i));
+            mNewWidgets.at(i)->visual()->setGeometry(calculatedRects.at(i));
             mNewWidgets.at(i)->savePresentation();
         }
     }
@@ -148,3 +168,46 @@
 {
     mNewWidgets.append(item);
 }
+
+#ifdef HSWIDGETORGANIZER_ALGORITHM
+// TODO: sorting should be done in algorithm class, make widget<->rect mapping here and move sortWidgets function to algorithm side
+/*!
+    Sorts widgets in height/width order
+*/
+QList<HsWidgetHost*> HsPageNewWidgetLayout::sortWidgets(sortOrder order)
+{
+    QList<HsWidgetHost*> tmpWidgets;
+
+    for ( int i = 0; i < mNewWidgets.count(); i++) {
+        int index = 0;
+        // add first widget to sorted list
+        if (i == 0) {
+            tmpWidgets << mNewWidgets.at(i);
+        } else {
+            // go through existing widgets in the sorted list
+            for ( int j = 0; j < tmpWidgets.count(); j++) {
+                // sort widgets in height order
+                if (order == height) {
+                    /* if widgets heigth is smaller on already
+                       existing ones in the list -> increment index
+                    */
+                    if (mNewWidgets.at(i)->visual()->preferredHeight() <= tmpWidgets.at(j)->visual()->preferredHeight()) {
+                        index++;
+                    }
+                // sort widgets in width order
+                } else {
+                    /* if widgets width is smaller on already
+                       existing ones in the sorted list -> increment index
+                    */
+                    if (mNewWidgets.at(i)->visual()->preferredWidth() <= tmpWidgets.at(j)->visual()->preferredWidth()) {
+                        index++;
+                    }
+                }
+            }
+            // add widget to its correct index in sorted list
+            tmpWidgets.insert(index, mNewWidgets.at(i));
+        }
+    }
+    return tmpWidgets;
+}
+#endif // HSWIDGETORGANIZER_ALGORITHM
\ No newline at end of file