homescreenapp/hsdomainmodel/src/hswidgethostvisual.cpp
changeset 77 4b195f3bea29
child 90 3ac3aaebaee5
equal deleted inserted replaced
61:2b1b11a301d2 77:4b195f3bea29
       
     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 <QGraphicsLinearLayout>
       
    19 #include <QParallelAnimationGroup>
       
    20 #include <QPropertyAnimation>
       
    21 #include <QGraphicsDropShadowEffect>
       
    22 #include <QGraphicsSceneResizeEvent>
       
    23 #include <QGesture>
       
    24 #include <QGraphicsScene>
       
    25 
       
    26 
       
    27 #include <HbInstantFeedback>
       
    28 #include <HbTouchArea>
       
    29 
       
    30 #include "hswidgethostvisual.h"
       
    31 #include "hswidgethost.h"
       
    32 #include "hsscene.h"
       
    33 #include "hsconfiguration.h"
       
    34 #include "hswidgettoucharea.h"
       
    35 
       
    36 
       
    37 /*!
       
    38     \class HsWidgetHostVisual
       
    39     \ingroup group_hsdomainmodel
       
    40     \brief Each widget is controlled by the home screen framework through a widget host.
       
    41 */
       
    42 
       
    43 /*!
       
    44     Constructs a new widget host with given \a databaseId and
       
    45     \a parent item.
       
    46 */
       
    47 HsWidgetHostVisual::HsWidgetHostVisual(QGraphicsItem *parent)
       
    48   : HbWidget(parent),mWidget(0)
       
    49   
       
    50 {
       
    51     setFlag(QGraphicsItem::ItemClipsChildrenToShape);
       
    52     setFlag(QGraphicsItem::ItemHasNoContents);
       
    53 
       
    54     grabGesture(Qt::TapGesture);
       
    55     grabGesture(Qt::TapAndHoldGesture);
       
    56     grabGesture(Qt::PanGesture);
       
    57     grabGesture(Qt::PinchGesture);
       
    58     grabGesture(Qt::SwipeGesture);
       
    59     grabGesture(Qt::CustomGesture);
       
    60     
       
    61     setupTouchArea();
       
    62     setupEffects();
       
    63     
       
    64     setMinimumSize(HSCONFIGURATION_GET(minimumWidgetSizeInPixels));
       
    65     setMaximumSize(HSCONFIGURATION_GET(maximumWidgetSizeInPixels));
       
    66     
       
    67 }
       
    68 
       
    69 /*!
       
    70     Destructor.
       
    71 */
       
    72 HsWidgetHostVisual::~HsWidgetHostVisual()
       
    73 {
       
    74 }
       
    75 
       
    76 
       
    77 /*!
       
    78     Reimplemented from QGraphicsItem. Returns the shape of the
       
    79     this widget host. The shape is computed based on the contained
       
    80     widget.
       
    81 */
       
    82 QPainterPath HsWidgetHostVisual::shape() const
       
    83 {
       
    84     QPainterPath path;
       
    85 
       
    86     if (mWidget) {
       
    87         QRectF currRect = rect();
       
    88         path = mWidget->shape();
       
    89 
       
    90         QRectF pathRect(path.boundingRect());
       
    91 
       
    92         if (pathRect.width() > currRect.width()
       
    93             || pathRect.height() > currRect.height()) {
       
    94             QPainterPath newPath(currRect.topLeft());
       
    95             newPath.addRect(currRect);
       
    96             path = path.intersected(newPath);
       
    97         }
       
    98     }
       
    99     return path;
       
   100 }
       
   101 void HsWidgetHostVisual::setWidget(QObject *widget)
       
   102 {
       
   103     mWidget = qobject_cast<QGraphicsWidget *>(widget);
       
   104     if (mWidget) {
       
   105         mWidget->installEventFilter(this);
       
   106         mWidget->setParentItem(this);
       
   107         setNewSize(mWidget->size());
       
   108     }
       
   109 }
       
   110 
       
   111 void HsWidgetHostVisual::setVisualModel(HsWidgetHost *model)
       
   112 {
       
   113     mVisualModel = model;
       
   114 }
       
   115 HsWidgetHost *HsWidgetHostVisual::visualModel() const
       
   116 {
       
   117     return mVisualModel;
       
   118 }
       
   119 /*!
       
   120     Starts the drag effect.
       
   121 */
       
   122 void HsWidgetHostVisual::startDragEffect()
       
   123 {
       
   124     /* TODO: Uncomment after the Qt bug has been fixed.
       
   125     QGraphicsDropShadowEffect *effect =
       
   126         static_cast<QGraphicsDropShadowEffect *>(graphicsEffect());
       
   127     */
       
   128     HbInstantFeedback::play(HbFeedback::ItemPick);
       
   129 
       
   130     setTransformOriginPoint(rect().center());
       
   131 
       
   132     QParallelAnimationGroup *animationGroup = new QParallelAnimationGroup();
       
   133 
       
   134     QPropertyAnimation *animation = new QPropertyAnimation(this, "scale");
       
   135     animation->setDuration(HSCONFIGURATION_GET(widgetDragEffectDuration));
       
   136     animation->setEndValue(1.1);
       
   137     animationGroup->addAnimation(animation);
       
   138 
       
   139     /* TODO: Uncomment after the Qt bug has been fixed.
       
   140     animation = new QPropertyAnimation(effect, "offset");
       
   141     animation->setDuration(200);
       
   142     animation->setEndValue(QPointF(8 ,8));
       
   143     animationGroup->addAnimation(animation);
       
   144     */
       
   145 
       
   146     animationGroup->start(QAbstractAnimation::DeleteWhenStopped);
       
   147 }
       
   148 
       
   149 /*!
       
   150     Starts the drop effect.
       
   151 */
       
   152 void HsWidgetHostVisual::startDropEffect()
       
   153 {
       
   154     /* TODO: Uncomment after the Qt bug has been fixed.
       
   155     QGraphicsDropShadowEffect *effect =
       
   156         static_cast<QGraphicsDropShadowEffect *>(graphicsEffect());
       
   157     */
       
   158     HbInstantFeedback::play(HbFeedback::ItemDrop);
       
   159 
       
   160     QParallelAnimationGroup *animationGroup = new QParallelAnimationGroup;
       
   161 
       
   162     QPropertyAnimation *animation = new QPropertyAnimation(this, "scale");
       
   163     animation->setDuration(HSCONFIGURATION_GET(widgetDropEffectDuration));
       
   164     animation->setEndValue(1);
       
   165     animationGroup->addAnimation(animation);
       
   166 
       
   167     /* TODO: Uncomment after the Qt bug has been fixed.
       
   168     animation = new QPropertyAnimation(effect, "offset");
       
   169     animation->setDuration(200);
       
   170     animation->setEndValue(QPointF(3, 3));
       
   171     animationGroup->addAnimation(animation);
       
   172     */
       
   173 
       
   174     animationGroup->start(QAbstractAnimation::DeleteWhenStopped);
       
   175 }
       
   176 /*!
       
   177     \fn HsWidgetHost::resized()
       
   178     Notifies the home screen framework that this widget
       
   179     host has resized itself.
       
   180 */
       
   181 
       
   182 /*!
       
   183     Reimplemented from QObject for monitoring changes in 
       
   184     contained widget's size.
       
   185 */
       
   186 bool HsWidgetHostVisual::eventFilter(QObject *watched, QEvent *event)
       
   187 {
       
   188     if (event->type() == QEvent::GraphicsSceneResize ) {
       
   189         QGraphicsSceneResizeEvent *resizeEvent = 
       
   190             static_cast<QGraphicsSceneResizeEvent *>(event);
       
   191         setNewSize(resizeEvent->newSize());
       
   192         emit resized();
       
   193     }
       
   194     return HbWidget::eventFilter(watched, event);
       
   195 }
       
   196 
       
   197 /*!
       
   198     Reimplemented from HbWidget for pan gesture handling.
       
   199 */
       
   200 void HsWidgetHostVisual::gestureEvent(QGestureEvent *event)
       
   201 {
       
   202     HsScene *scene = HsScene::instance();    
       
   203     QGesture *gesture = event->gesture(Qt::PanGesture);
       
   204     if (gesture) {
       
   205         switch (gesture->state()) {
       
   206             case Qt::GestureStarted:
       
   207                 grabMouse();
       
   208                 emit scene->pagePanStarted(event);
       
   209                 break;
       
   210             case Qt::GestureUpdated:
       
   211                 emit scene->pagePanUpdated(event);
       
   212                 break;
       
   213             case Qt::GestureFinished:
       
   214             case Qt::GestureCanceled:
       
   215                 ungrabMouse();
       
   216                 emit scene->pagePanFinished(event);
       
   217                 break;
       
   218             default:
       
   219                 break;
       
   220         }
       
   221     }
       
   222 }
       
   223 
       
   224 /*!
       
   225     \fn HsWidgetHostVisual::mousePressEvent(QGraphicsSceneMouseEvent *)
       
   226     Reimplemented from QGraphicsItem for eating all mouse presses.
       
   227 */
       
   228 
       
   229 /*!
       
   230     Configures the touch are for this widget host.
       
   231 */
       
   232 void HsWidgetHostVisual::setupTouchArea()
       
   233 {
       
   234     mTouchArea = new HsWidgetTouchArea(this);
       
   235     mTouchArea->setZValue(1);
       
   236 }
       
   237 
       
   238 /*!
       
   239     Configures the effects for this widget host.
       
   240 */
       
   241 void HsWidgetHostVisual::setupEffects()
       
   242 {
       
   243     /* TODO: Uncomment after the Qt bug has been fixed.
       
   244     QGraphicsDropShadowEffect *effect =
       
   245         new QGraphicsDropShadowEffect(this);
       
   246     effect->setColor(QColor(0, 0, 0, 150));
       
   247     effect->setBlurRadius(5);
       
   248     effect->setOffset(3);
       
   249     setGraphicsEffect(effect);
       
   250     */
       
   251 }
       
   252 
       
   253 
       
   254 /*!
       
   255     Resizes this widget host to the given \a size.
       
   256 */
       
   257 void HsWidgetHostVisual::setNewSize(const QSizeF &size)
       
   258 {
       
   259     mTouchArea->resize(size);
       
   260     resize(size);
       
   261     setPreferredSize(size);
       
   262 }
       
   263