homescreenapp/examples/pannablewidgetplugin/src/pannablewidget.cpp
changeset 62 341166945d65
parent 57 2e2dc3d30ca8
child 63 52b0f64eeb51
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
     1 /*
       
     2 * Copyright (c) 2010 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:  Example of home screen widget
       
    15 *
       
    16 */
       
    17 #include <qtDebug>
       
    18 
       
    19 #include <QGraphicsLinearLayout>
       
    20 #include <HbPushButton>
       
    21 #include <HbSlider>
       
    22 #include <QGraphicsSceneMouseEvent>
       
    23 #include <QGraphicsRectItem>
       
    24 #include "pannablewidget.h"
       
    25 
       
    26 
       
    27 
       
    28 /*!
       
    29     \ingroup group_pannablewidget
       
    30     \class PannableWidget
       
    31     \brief Example implementation for pannable home screen widget.
       
    32 
       
    33     Pannable widget demostrates usage of horizontal pan gesture instead of Home Screen using it for
       
    34     page change.
       
    35     
       
    36     To enable horizontal pan gesture widget need to implement isPannable() function. Widget should return true if pannable item's
       
    37     pan region contains event position.  
       
    38 */
       
    39 
       
    40 
       
    41 const int ItemName = 0xfffe;
       
    42 
       
    43 /*!
       
    44     Constructs a widget which is a child of \a parent, with widget flags set to \a flags.
       
    45 */
       
    46 PannableWidget::PannableWidget(QGraphicsItem* parent, Qt::WindowFlags flags)
       
    47     : HbWidget(parent, flags)
       
    48 {
       
    49     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
       
    50     layout->setContentsMargins(0.0, 0.0, 0.0, 0.0);
       
    51     setLayout(layout);
       
    52     
       
    53     HbPushButton *button = new HbPushButton("Jenson Button");
       
    54     button->setPreferredSize(QSizeF(200.0,20.0));
       
    55     layout->addItem(button);
       
    56 
       
    57     HbSlider *slider = new HbSlider(Qt::Horizontal);
       
    58     slider->setData(ItemName,QLatin1String("slider"));
       
    59     slider->setRange(0,10);
       
    60     slider->setValue(5);
       
    61     layout->addItem(slider);
       
    62 
       
    63     
       
    64     QGraphicsRectItem *background = new QGraphicsRectItem();
       
    65     background->setBrush(QBrush(QColor(0x9acd32)));
       
    66     setBackgroundItem(background);
       
    67     
       
    68     resize(200.0, 100.0);
       
    69     background->setRect(rect());
       
    70 
       
    71 }
       
    72 
       
    73 /*!
       
    74     Destructor
       
    75 */
       
    76 PannableWidget::~PannableWidget()
       
    77 {
       
    78 }
       
    79 /*!
       
    80     Called by Home Screen when pan event is recognized. Given \a event
       
    81     can be used to check whether pannable item contains event position.
       
    82 */
       
    83 bool PannableWidget::isPannable(QGraphicsSceneMouseEvent *event)
       
    84 {
       
    85     bool ret(false);
       
    86     // Take position in scene coordinates
       
    87     QPointF position = event->buttonDownScenePos(Qt::LeftButton);
       
    88     // find pannable item
       
    89     QGraphicsItem* item = findChildItemByName(this,"slider");
       
    90     // check whether it is event is inside it
       
    91     if (item && item->sceneBoundingRect().contains(position)) {
       
    92         ret = true;
       
    93     }
       
    94     return ret;
       
    95 }
       
    96 /*!
       
    97     Find \a item childs by given \a name. Uses recursion.
       
    98 */
       
    99 QGraphicsItem *PannableWidget::findChildItemByName(QGraphicsItem *item, const QString &name)
       
   100 {
       
   101     if (!item) return NULL;
       
   102 
       
   103     QList<QGraphicsItem *> children = item->childItems();
       
   104     foreach (QGraphicsItem *child, children) {
       
   105         if(name == child->data(ItemName).toString()) {
       
   106             return child;
       
   107         }
       
   108     }
       
   109     // do recursive search
       
   110     foreach (QGraphicsItem *child, children) {
       
   111         QGraphicsItem* itemFromChild = findChildItemByName(child,name);
       
   112         if (itemFromChild) {
       
   113             return itemFromChild;
       
   114         }
       
   115     }
       
   116     return NULL;
       
   117 }
       
   118 
       
   119 
       
   120 /*!
       
   121     Called when widget is shown in the home screen
       
   122 */
       
   123 void PannableWidget::onShow()
       
   124 {
       
   125 }
       
   126 
       
   127 /*!
       
   128     Called when widget is hidden from the home screen
       
   129 */
       
   130 void PannableWidget::onHide()
       
   131 {
       
   132 }