src/hbcore/gui/hblongpressvisualizer.cpp
changeset 1 f7ac710697a9
parent 0 16d8024aca5e
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
    21 ** If you have questions regarding the use of this file, please contact
    21 ** If you have questions regarding the use of this file, please contact
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 
       
    26 #include "hblongpressvisualizer.h"
       
    27 #include "hblongpressvisualizer_p.h"
       
    28 #include <hbiconitem.h>
       
    29 #include <hbmainwindow.h>
       
    30 #include <hbwidget.h>
       
    31 #include <hbinstance.h>
       
    32 #include <hbiconanimationmanager.h>
       
    33 #include <hbiconanimator.h>
       
    34 #include <hbwidgetfeedback.h>
       
    35 #include <QGraphicsScene>
       
    36 
    26 /*!
    37 /*!
    27 //
    38   \class HbLongPressVisualizer
    28 //  W A R N I N G
    39 
    29 //  -------------
    40   \brief Displays the long press animation, that is, the small animated icon
    30 //
    41   indicating that the tap being held may potentially become a long press.
    31 // This implementation of longpress visualizer is most probably removed in later releases.
    42 
    32 // It exists purely as an implementation detail.
    43   Widgets or applications should use the functions of this class to show and
    33 // This implementation may change from version to
    44   hide the standard, themed long press animation.
    34 // version without notice, or even be removed.
       
    35 //
       
    36 // We mean it.
       
    37 //
       
    38 */
    45 */
    39 
    46 
    40 #include "hblongpressvisualizer_p.h"
    47 Q_GLOBAL_STATIC(HbLongPressVisualizerPrivate, visualizer)
    41 
    48 
    42 #include <QPen>
    49 /*!
    43 #include <QPainter>
    50   Shows the animated icon at (or near) position \a pos. If \a delayMs is non-zero then
       
    51   the animation is only shown after the specified number of milliseconds.
    44 
    52 
    45 /* 
    53   \a pos is treated to be a scene position. In typical usage scenarios the scene
    46 	HbLongPressVisualizer
    54   position will be retrieved from a gesture event and passed as it is in \a
    47 	HbLongPressVisualizer is part of the internal implementation. It is not supposed to be in public API.
    55   pos. Do not make any assumptions about the exact position of the icon, the
    48 	
    56   visualizer may decide to position it a bit differently in order to make it
    49 	
    57   more visible to the user (e.g. to prevent being obscured by the user's
    50 	HbLongPressVisualizer is a visualizer for the long-press gesture.
    58   finger).
    51 
    59 
    52 	HbGestureFilter
    60   If \a widget is not 0 then the icon is added to the scene of the widget's
    53 */
    61   mainwindow.  Otherwise the first main window is used. If no main windows were
       
    62   instantiated before calling start() then it will return immediately.
    54 
    63 
       
    64   The widget is also used for tactile feedback, if it is 0 then no feedback
       
    65   effect will be started. If the widget is given then the instant LongPressed
       
    66   feedback effect will be started automatically.
       
    67  */
       
    68 void HbLongPressVisualizer::start(const QPointF &pos, int delayMs, const HbWidget *widget)
       
    69 {
       
    70     visualizer()->start(pos, delayMs, widget);
       
    71 }
    55 
    72 
    56 /*
    73 /*!
    57 	Constructs sample long-press visualization widget with a parent.
    74   Hides the animated icon if it is visible. Has no effect if the animation is
    58 */
    75   not currently visible.
       
    76  */
       
    77 void HbLongPressVisualizer::stop()
       
    78 {
       
    79     visualizer()->stop();
       
    80 }
    59 
    81 
    60 HbLongPressVisualizer::HbLongPressVisualizer( QGraphicsItem *parent ) : HbWidget(parent), active(false), spanAngle(0)
    82 HbLongPressVisualizerPrivate::HbLongPressVisualizerPrivate()
       
    83     : mInited(false), mWidget(0), mIconItem(0)
    61 {
    84 {
    62 }
    85 }
    63 
    86 
    64 /*
    87 void HbLongPressVisualizerPrivate::start(const QPointF &pos, int delayMs, const HbWidget *widget)
    65 	Reimplemented from QGraphicsItem::paint().
       
    66 */
       
    67 void HbLongPressVisualizer::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    68 {
    88 {
    69     Q_UNUSED(option);
    89     // Multiple mainwindow support is in place below, however currently there is
    70     Q_UNUSED(widget);
    90     // only one icon item so the icon is only shown in one mainwindow at a time.
    71     
    91     stop();
    72     if (active) {
    92     mWidget = widget;
    73         QPen pen( Qt::lightGray );
    93     HbMainWindow *mainWindow = widget ? widget->mainWindow() : 0;
    74         pen.setWidth(5);
    94     if (!mainWindow) {
    75         painter->setPen(pen);
    95         QList<HbMainWindow *> mainWindows(hbInstance->allMainWindows());
    76         painter->drawArc(rect, 90*16, -spanAngle*16);
    96         if (!mainWindows.isEmpty()) {
       
    97             mainWindow = mainWindows.at(0);
       
    98         } else {
       
    99             return;
       
   100         }
       
   101     }
       
   102     if (!mInited) {
       
   103         mInited = true;
       
   104         mTimer.setSingleShot(true);
       
   105         connect(&mTimer, SIGNAL(timeout()), SLOT(showIcon()));
       
   106         HbIconAnimationManager::global()->addDefinitionFile("qtg_anim_longtap.axml");
       
   107         mIconItem = new HbIconItem("qtg_anim_longtap");
       
   108         mIconItem->hide();
       
   109         mIconItem->setSize(mIconItem->defaultSize());
       
   110     }
       
   111     QGraphicsScene *targetScene = mainWindow->scene();
       
   112     QGraphicsScene *oldScene = mIconItem->scene();
       
   113     if (targetScene != oldScene) {
       
   114         if (oldScene) {
       
   115             oldScene->removeItem(mIconItem);
       
   116         }
       
   117         targetScene->addItem(mIconItem); // takes ownership
       
   118     }
       
   119     prepareIcon(pos);
       
   120     if (delayMs > 0) {
       
   121         mTimer.start(delayMs);
       
   122     } else {
       
   123         showIcon();
    77     }
   124     }
    78 }
   125 }
    79 
   126 
    80 /*
   127 void HbLongPressVisualizerPrivate::stop()
    81 	Start HbLongPressVisualizer.
       
    82 
       
    83 	Currently position is hardcoded.
       
    84 */
       
    85 void HbLongPressVisualizer::start(QPointF scenePos)
       
    86 {
   128 {
    87     prepareGeometryChange();
   129     mTimer.stop();
    88     rect = QRect( 0, 0, 30, 30);
   130     if (mIconItem) {
    89 
   131         mIconItem->animator().stopAnimation();
    90     if (scenePos.y() < 60 ) {
   132         mIconItem->hide();
    91         //Draw the animation below of the touch point
       
    92         rect.moveCenter( QPointF(scenePos.x(), scenePos.y()+50));
       
    93     }
   133     }
    94     else {
       
    95         //Draw the animation above of the touch point
       
    96         rect.moveCenter( QPointF(scenePos.x(), scenePos.y()-50));
       
    97     }
       
    98 
       
    99     setFrame(0);
       
   100     active = true;
       
   101 }
   134 }
   102 
   135 
   103 /*
   136 void HbLongPressVisualizerPrivate::prepareIcon(const QPointF &pos)
   104 	Stop HbLongPressVisualizer.
       
   105 */
       
   106 void HbLongPressVisualizer::stop()
       
   107 {
   137 {
   108     active = false;
   138     mPos = pos;
   109     update();
   139     QSizeF iconSize = mIconItem->size();
       
   140     mPos -= QPointF(iconSize.width() / 2, iconSize.height());
       
   141     mIconItem->setPos(mPos);
   110 }
   142 }
   111 
   143 
   112 
   144 void HbLongPressVisualizerPrivate::showIcon()
   113 /*
       
   114 	Set frame for HbLongPressVisualizer
       
   115 */
       
   116 void HbLongPressVisualizer::setFrame(int frame)
       
   117 {
   145 {
   118     spanAngle = frame*360/100;
   146     mIconItem->animator().startAnimation();
   119     update();
   147     mIconItem->show();
       
   148     if (mWidget) {
       
   149         HbWidgetFeedback::triggered(mWidget, Hb::InstantLongPressed);
       
   150     }
   120 }
   151 }
   121 
       
   122 /*
       
   123 	Reimplemented from QGraphicsItem::boundingRect().
       
   124 
       
   125 	Currently returns the default bounding rect.
       
   126 
       
   127 */
       
   128 QRectF HbLongPressVisualizer::boundingRect() const 
       
   129 {
       
   130     return rect;
       
   131 }