homescreenapp/hsdomainmodel/src/hspage.cpp
changeset 63 52b0f64eeb51
parent 62 341166945d65
child 69 87476091b3f5
--- a/homescreenapp/hsdomainmodel/src/hspage.cpp	Fri Jun 25 19:19:22 2010 +0300
+++ b/homescreenapp/hsdomainmodel/src/hspage.cpp	Fri Jul 09 14:36:01 2010 +0300
@@ -48,13 +48,18 @@
       mDatabaseId(-1),
       mWallpaper(0),
       mRemovable(true),
-      mTouchArea(0)
+      mTouchArea(0),
+      mPageMargin(0.0)
 {
     setFlag(QGraphicsItem::ItemHasNoContents);
     setSizePolicy(QSizePolicy(QSizePolicy::Ignored,
                               QSizePolicy::Ignored));
 
     setupTouchArea();
+
+    //Page margin
+    mPageMargin = HSCONFIGURATION_GET(pageMargin);
+    connect(HsConfiguration::instance(), SIGNAL(propertyChanged(QString)), SLOT(onPageMarginChanged(QString)));
 }
 
 /*!
@@ -127,7 +132,7 @@
 }
 
 /*!
-    Return wallpaper. 
+    Return wallpaper.
 */
 HsWallpaper *HsPage::wallpaper() const
 {
@@ -156,6 +161,7 @@
 
     return true;
  }
+
 /*!
     Remove given \a widgetHost from a page. Widget is not deleted. 
     Returns true if successful
@@ -172,6 +178,7 @@
 
     return true;
 }
+
 /*!
     Returns list of new widgets belonging to a page. Widgets which are
     not yet layouted are considered as new widgets. 
@@ -197,16 +204,17 @@
     }
 
     HsWidgetPresentationData presentation;
+    QPointF adjustedWidgetPos = adjustedWidgetPosition(widgetHost->geometry());
     presentation.orientation = HsScene::orientation();
     if (!widgetHost->getPresentation(presentation)) {
         presentation.orientation = HsScene::orientation();
-        presentation.setPos(mTouchPoint);
+        presentation.setPos(adjustedWidgetPos);
         presentation.zValue = 0;
         widgetHost->savePresentation(presentation);
     }
 
     widgetHost->hide();
-    widgetHost->setPos(presentation.x, presentation.y);
+    widgetHost->setPos(adjustedWidgetPos);
     widgetHost->setZValue(presentation.zValue);
 
     connectWidget(widgetHost);
@@ -244,6 +252,15 @@
 }
 
 /*!
+    Clears new widgets list and resets layout.
+*/
+void HsPage::resetNewWidgets()
+{
+    mNewWidgets.clear();
+    setLayout(0);
+}
+
+/*!
     Remove page and all it's contained widgets from database 
 */
 bool HsPage::deleteFromDatabase()
@@ -262,9 +279,13 @@
         widget->remove();
     }
     mUnavailableWidgets.clear();
-
+    
+    if (mWallpaper) {
+        mWallpaper->remove();
+    }
     return HsDatabase::instance()->deletePage(mDatabaseId);
 }
+
 /*!
     Return list of widgets belonging to a page 
 */
@@ -289,6 +310,7 @@
 {
     mRemovable = removable;
 }
+
 /*!
     Return true if page is default page.
 */
@@ -296,6 +318,7 @@
 {
     return mDatabaseId == HSCONFIGURATION_GET(defaultPageId);
 }
+
 /*!
     Return true if page is active page.
 */
@@ -303,6 +326,7 @@
 {
     return this == HsScene::instance()->activePage();
 }
+
 /*!
     Create page into database and return instance of a new page.
 */
@@ -322,6 +346,65 @@
 }
 
 /*!
+    The widget is bounded in the rectangle which is smaller by pageMargin on all sides of page.
+*/
+QPointF HsPage::adjustedWidgetPosition(const QRectF &origWidgetRect)
+{
+    QRectF widgetAreaRect = contentGeometry();
+    qreal widgetX = qBound(widgetAreaRect.left(), origWidgetRect.x(), widgetAreaRect.right() - origWidgetRect.width());
+    qreal widgetY = qBound(widgetAreaRect.top(), origWidgetRect.y(), widgetAreaRect.bottom() - origWidgetRect.height());
+
+    return QPointF(widgetX, widgetY);
+}
+
+/*!
+    Returns rect of rectangular where widgets are allowed to be placed in the page.
+*/
+QRectF HsPage::contentGeometry()
+{
+    return contentGeometry(HsScene::mainWindow()->orientation());
+}
+
+/*!
+    Returns rect of rectangular where widgets are allowed to be placed in the page.
+*/
+QRectF HsPage::contentGeometry(Qt::Orientation orientation)
+{
+    QRectF pageRect = HsScene::mainWindow()->layoutRect();
+
+    if (orientation != HsScene::orientation()) {
+        qreal width = pageRect.width();
+        qreal height = pageRect.height();
+        pageRect.setWidth(height);
+        pageRect.setHeight(width);
+    }
+
+    //Take care of chrome in both orientation
+    pageRect.setTop(64);
+
+    //Shrink by page margins at each side
+    return pageRect.adjusted(mPageMargin, mPageMargin, -mPageMargin, -mPageMargin);
+}
+
+/*!
+    Returns rect of rectangular where widgets are allowed to be placed in the page.
+*/
+QRectF HsPage::contentRect()
+{
+    return contentRect(HsScene::mainWindow()->orientation());
+}
+
+/*!
+    Returns rect of rectangular where widgets are allowed to be placed in the page.
+*/
+QRectF HsPage::contentRect(Qt::Orientation orientation)
+{
+    QRectF rect = contentGeometry(orientation);
+    rect.moveTopLeft(QPointF(0,0));
+    return rect;
+}
+
+/*!
     Calls onShow() for contained widgets.
 */
 void HsPage::showWidgets()
@@ -346,7 +429,7 @@
 }
 
 /*!
-    Propogate online state to widgets.
+    Propagate online state to widgets.
 */
 void HsPage::setOnline(bool online)
 {
@@ -357,6 +440,7 @@
         widget->setOnline(online);
     }
 }
+
 /*!
     Update widgets z-values and persist those. Active widget has top most 
     z-value.
@@ -457,11 +541,7 @@
 {
     if ( !layout() ) {
         HsWidgetHost *widget = qobject_cast<HsWidgetHost *>(sender());
-        QRectF widgetRect = widget->geometry();
-        QRectF pageRect = HsScene::mainWindow()->layoutRect();
-        qreal widgetX = qBound(qreal(0), widgetRect.x(), pageRect.width() - widgetRect.width());
-        qreal widgetY = qBound(qreal(64), widgetRect.y(), pageRect.height() - widgetRect.height());
-        widget->setPos(widgetX, widgetY);
+        widget->setPos(adjustedWidgetPosition(widget->geometry()));
     } else {
         layout()->invalidate();
     }
@@ -500,20 +580,19 @@
     widget->hide();
     widget->setParentItem(0);
 }
+
 /*!
     Run positioning algorithm for widgets which don't have position on 
     target orientation. Otherwise set orientation positions for widgets.
 */
 void HsPage::onOrientationChanged(Qt::Orientation orientation)
 {
-    QRectF rect = HsScene::mainWindow()->layoutRect();
-
     HsWidgetPositioningOnOrientationChange *converter =
         HsWidgetPositioningOnOrientationChange::instance();
 
-    qreal chrome = 64;
-    QRectF from(0, chrome, rect.height(), rect.width() - chrome);
-    QRectF to(0, chrome, rect.width(), rect.height() - chrome);
+    Qt::Orientation orientationFrom = orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical;
+    QRectF from = contentGeometry(orientationFrom);
+    QRectF to = contentGeometry(orientation);
 
     HsWidgetPresentationData presentation;
     presentation.orientation = orientation;
@@ -525,18 +604,33 @@
             widget->setGeometry(geometries.first());
             widget->savePresentation();
         } else {
-            widget->setPos(presentation.pos());
+            QRectF adjustWidgetPosition;
+            adjustWidgetPosition = widget->geometry();
+            adjustWidgetPosition.moveTopLeft(presentation.pos());
+            widget->setPos(adjustedWidgetPosition(adjustWidgetPosition));
             widget->setZValue(presentation.zValue);
+            widget->savePresentation(); //Needed to follow pageMargin dynamic change
         }
     }
 }
 
-/*!
-    Clears new widgets list and resets layout.
-*/
-void HsPage::resetNewWidgets()
+void HsPage::onPageMarginChanged(const QString &value)
 {
-    mNewWidgets.clear();
-    setLayout(0);
+    if (value == "pageMargin") {
+        mPageMargin = HSCONFIGURATION_GET(pageMargin);
+
+        if (!mWidgets.isEmpty()) {
+            foreach (HsWidgetHost *widget, mWidgets) {
+                widget->setPos(adjustedWidgetPosition(widget->geometry()));
+                widget->savePresentation();
+            }
+        }
+
+        if (!mNewWidgets.isEmpty()) {
+            foreach (HsWidgetHost *widget, mNewWidgets) {
+                widget->setPos(adjustedWidgetPosition(widget->geometry()));
+                widget->savePresentation();
+            }
+        }
+    }
 }
-