homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp
changeset 69 87476091b3f5
parent 63 52b0f64eeb51
child 81 7dd137878ff8
--- a/homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp	Wed Jul 14 15:53:30 2010 +0300
+++ b/homescreenapp/hsdomainmodel/src/hspagenewwidgetlayout.cpp	Fri Jul 23 13:47:57 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,18 +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"
 
 
 /*!
@@ -46,7 +46,7 @@
     : QGraphicsLayout(parent),    
     mTouchPoint(touchPoint)
 {
-    mSize = HsScene::mainWindow()->layoutRect().size();
+    mSize = HsGui::instance()->layoutRect().size();
 }
 
 /*!
@@ -69,7 +69,7 @@
 */
 QGraphicsLayoutItem *HsPageNewWidgetLayout::itemAt(int i) const
 {
-    return mNewWidgets.at(i);
+    return mNewWidgets.at(i)->visual();
 }
 
 /*!
@@ -103,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)
@@ -114,25 +124,38 @@
        -> set widget center point to this touch point
     */
     if (mTouchPoint != QPointF() && mNewWidgets.count() == 1) {
-        QRectF widgetRect = rects.at(0);
+        QRectF widgetRect = newRects.at(0);
         widgetRect.moveCenter(mTouchPoint);
         widgetRect.moveTopLeft(HsScene::instance()->activePage()->adjustedWidgetPosition(widgetRect));
-        mNewWidgets.at(0)->setGeometry(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::instance()->activePage()->contentGeometry();
         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();
         }
     }
@@ -145,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