calendarui/views/dayview/src/calendaycontentscrollarea.cpp
author hgs
Tue, 31 Aug 2010 18:15:13 +0530
changeset 64 1881ad52dc45
parent 63 a3cb48f6c889
child 70 a5ed90760192
permissions -rw-r--r--
201033_03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
 * All rights reserved.
hgs
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
 * under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
 *
hgs
parents:
diff changeset
     9
 * Initial Contributors:
hgs
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
 *
hgs
parents:
diff changeset
    12
 * Contributors:
hgs
parents:
diff changeset
    13
 *
hgs
parents:
diff changeset
    14
 * Description:  CalenDayContentScrollArea implementation.
hgs
parents:
diff changeset
    15
 *
hgs
parents:
diff changeset
    16
 */
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
// System includes
hgs
parents:
diff changeset
    19
#include <QGraphicsLinearLayout>
hgs
parents:
diff changeset
    20
#include <QGesture>
hgs
parents:
diff changeset
    21
63
hgs
parents: 57
diff changeset
    22
#include <HbInstance>
hgs
parents: 57
diff changeset
    23
#include <HbSwipeGesture>
45
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
// User includes
hgs
parents:
diff changeset
    26
#include "calendaycontentscrollarea.h"
hgs
parents:
diff changeset
    27
#include "calendayutils.h"
hgs
parents:
diff changeset
    28
57
hgs
parents: 55
diff changeset
    29
// Constants
hgs
parents: 55
diff changeset
    30
/*!
hgs
parents: 55
diff changeset
    31
 Default timeout for scrolling between days [ms]
hgs
parents: 55
diff changeset
    32
 */
hgs
parents: 55
diff changeset
    33
const int KCalenScrollDaysTimeout = 600;
hgs
parents: 55
diff changeset
    34
hgs
parents: 55
diff changeset
    35
/*!
hgs
parents: 55
diff changeset
    36
 Value [%] defines how long (depending on content area width) should horizontal
hgs
parents: 55
diff changeset
    37
 pan gesture be to change day to previous/next.
hgs
parents: 55
diff changeset
    38
 If the gesture is shorter - current view is not changed.
hgs
parents: 55
diff changeset
    39
 */
hgs
parents: 55
diff changeset
    40
const int KCalenHScrollMoveParam = 30;  //!< Percentage
hgs
parents: 55
diff changeset
    41
hgs
parents: 55
diff changeset
    42
/*!
hgs
parents: 55
diff changeset
    43
 Value [degree] defines the max. angle of swipe gesture which should change day.
hgs
parents: 55
diff changeset
    44
 */
hgs
parents: 55
diff changeset
    45
const qreal KCalenSwipeAngle = 30;
hgs
parents: 55
diff changeset
    46
hgs
parents: 55
diff changeset
    47
45
hgs
parents:
diff changeset
    48
/*!
hgs
parents:
diff changeset
    49
 \class CalenDayContentScrollArea
hgs
parents:
diff changeset
    50
 \brief Scrollable container class for content widgets.
hgs
parents:
diff changeset
    51
 
hgs
parents:
diff changeset
    52
 It handles horizontal scrolling and swipe or pan gestures.
hgs
parents:
diff changeset
    53
 */
hgs
parents:
diff changeset
    54
hgs
parents:
diff changeset
    55
/*!
hgs
parents:
diff changeset
    56
 \brief Constructor
hgs
parents:
diff changeset
    57
 
hgs
parents:
diff changeset
    58
 Configures scroll area settings and resets internal stares of widget.
hgs
parents:
diff changeset
    59
 Gets the width of device.
hgs
parents:
diff changeset
    60
 
hgs
parents:
diff changeset
    61
 \param parent The parent of scroll area widget
hgs
parents:
diff changeset
    62
 */
hgs
parents:
diff changeset
    63
CalenDayContentScrollArea::CalenDayContentScrollArea(QGraphicsItem *parent) :
hgs
parents:
diff changeset
    64
    HbScrollArea(parent), mPanDayDirection(ECalenPanNotSet), mIsMoving(false),
hgs
parents:
diff changeset
    65
    mMoveDirection(ECalenScrollNoDayChange)
55
hgs
parents: 45
diff changeset
    66
{ 
45
hgs
parents:
diff changeset
    67
#ifdef CALENDAYVIEW_PANNING_ENABLED
hgs
parents:
diff changeset
    68
    grabGesture(Qt::PanGesture);
hgs
parents:
diff changeset
    69
    ungrabGesture(Qt::SwipeGesture);
hgs
parents:
diff changeset
    70
#else
hgs
parents:
diff changeset
    71
    grabGesture(Qt::SwipeGesture);
hgs
parents:
diff changeset
    72
    ungrabGesture(Qt::PanGesture);
hgs
parents:
diff changeset
    73
#endif
hgs
parents:
diff changeset
    74
hgs
parents:
diff changeset
    75
    // Get the width of content area and orientation of screen
hgs
parents:
diff changeset
    76
    mContentWidth = CalenDayUtils::instance()->contentWidth();
hgs
parents:
diff changeset
    77
    mOrientation = CalenDayUtils::instance()->orientation();
55
hgs
parents: 45
diff changeset
    78
    
hgs
parents: 45
diff changeset
    79
    // Fix the width of scroll area
hgs
parents: 45
diff changeset
    80
    setMinimumWidth(mContentWidth);
hgs
parents: 45
diff changeset
    81
    setMaximumWidth(mContentWidth);
45
hgs
parents:
diff changeset
    82
hgs
parents:
diff changeset
    83
    // Connect to main window's orientationChanged SIGNAL to handle orientation
hgs
parents:
diff changeset
    84
    // switching
hgs
parents:
diff changeset
    85
    connect(CalenDayUtils::instance()->mainWindow(), 
hgs
parents:
diff changeset
    86
        SIGNAL(orientationChanged(Qt::Orientation)), this,
hgs
parents:
diff changeset
    87
        SLOT(orientationChanged(Qt::Orientation)));
hgs
parents:
diff changeset
    88
}
hgs
parents:
diff changeset
    89
hgs
parents:
diff changeset
    90
/*!
hgs
parents:
diff changeset
    91
 \brief Destructor
hgs
parents:
diff changeset
    92
 */
hgs
parents:
diff changeset
    93
CalenDayContentScrollArea::~CalenDayContentScrollArea()
hgs
parents:
diff changeset
    94
{
hgs
parents:
diff changeset
    95
}
hgs
parents:
diff changeset
    96
hgs
parents:
diff changeset
    97
/*!
hgs
parents:
diff changeset
    98
 \brief Scrolls to middle widget.
hgs
parents:
diff changeset
    99
 
hgs
parents:
diff changeset
   100
 Scrolling to middle widget is done if needed.
hgs
parents:
diff changeset
   101
 Resets internal pan direction flag.
hgs
parents:
diff changeset
   102
 */
hgs
parents:
diff changeset
   103
void CalenDayContentScrollArea::scrollToMiddleWidget()
hgs
parents:
diff changeset
   104
{
hgs
parents:
diff changeset
   105
    QPointF currentPosition = contentWidget()->pos();
hgs
parents:
diff changeset
   106
    QPointF destPosition = QPointF(mContentWidth, currentPosition.y());
hgs
parents:
diff changeset
   107
hgs
parents:
diff changeset
   108
    // Scroll only when x position is wrong
hgs
parents:
diff changeset
   109
    if (currentPosition.x() != destPosition.x()) {
hgs
parents:
diff changeset
   110
        scrollContentsTo(QPointF(mContentWidth, currentPosition.y()), 0);
hgs
parents:
diff changeset
   111
    }
hgs
parents:
diff changeset
   112
hgs
parents:
diff changeset
   113
    // Reset pan direction flag and scrolling flag
hgs
parents:
diff changeset
   114
    mPanDayDirection = ECalenPanNotSet;
hgs
parents:
diff changeset
   115
}
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
/*!
hgs
parents:
diff changeset
   118
 \brief Scrolls the view by the amount indicated by "delta".
hgs
parents:
diff changeset
   119
 
hgs
parents:
diff changeset
   120
 Checks the direction of pan gesture and promotes leading direction.
hgs
parents:
diff changeset
   121
 
hgs
parents:
diff changeset
   122
 \param delta Move offset
hgs
parents:
diff changeset
   123
 \return Returns TRUE if the view was able to scroll, FALSE otherwise
hgs
parents:
diff changeset
   124
 */
hgs
parents:
diff changeset
   125
bool CalenDayContentScrollArea::scrollByAmount(const QPointF &delta)
hgs
parents:
diff changeset
   126
{
hgs
parents:
diff changeset
   127
    QPointF newDelta(delta);
hgs
parents:
diff changeset
   128
    if (mPanDayDirection == ECalenPanVertical) {
hgs
parents:
diff changeset
   129
        newDelta.setX(0);
hgs
parents:
diff changeset
   130
    }
hgs
parents:
diff changeset
   131
    else
hgs
parents:
diff changeset
   132
        if (mPanDayDirection == ECalenPanHorizontal) {
hgs
parents:
diff changeset
   133
            newDelta.setY(0);
hgs
parents:
diff changeset
   134
        }
hgs
parents:
diff changeset
   135
        else {
hgs
parents:
diff changeset
   136
            // Pan direction not set
hgs
parents:
diff changeset
   137
        }
hgs
parents:
diff changeset
   138
hgs
parents:
diff changeset
   139
    return HbScrollArea::scrollByAmount(newDelta);
hgs
parents:
diff changeset
   140
}
hgs
parents:
diff changeset
   141
hgs
parents:
diff changeset
   142
/*!
hgs
parents:
diff changeset
   143
 \brief Handles pan gesture event (horizontal) or swipe gesture. 
hgs
parents:
diff changeset
   144
 
hgs
parents:
diff changeset
   145
 Ignores vertical pan gestures.
hgs
parents:
diff changeset
   146
 
hgs
parents:
diff changeset
   147
 \param event Gesture event to be handled
hgs
parents:
diff changeset
   148
 */
hgs
parents:
diff changeset
   149
void CalenDayContentScrollArea::gestureEvent(QGestureEvent *event)
hgs
parents:
diff changeset
   150
{
hgs
parents:
diff changeset
   151
#ifdef CALENDAYVIEW_PANNING_ENABLED
hgs
parents:
diff changeset
   152
    // Process a pan gesture event
hgs
parents:
diff changeset
   153
    if (QPanGesture *panGesture = qobject_cast<QPanGesture*> (event->gesture(
hgs
parents:
diff changeset
   154
        Qt::PanGesture))) {
hgs
parents:
diff changeset
   155
hgs
parents:
diff changeset
   156
        // Checks pan gesture direction
hgs
parents:
diff changeset
   157
        checkPanDirection(panGesture);
hgs
parents:
diff changeset
   158
hgs
parents:
diff changeset
   159
        // Put the gesture forward before working with finished gesture
hgs
parents:
diff changeset
   160
        HbScrollArea::gestureEvent(event);
hgs
parents:
diff changeset
   161
hgs
parents:
diff changeset
   162
        // If gesture is finished move the scroll area to next or previous 
hgs
parents:
diff changeset
   163
        // widget or resume to gesture start point
hgs
parents:
diff changeset
   164
        if (panGesture->state() == Qt::GestureFinished) {
hgs
parents:
diff changeset
   165
            // Pan direction should be reseted when scrolling ends
hgs
parents:
diff changeset
   166
hgs
parents:
diff changeset
   167
            // Gets the offset of pan gesture.
hgs
parents:
diff changeset
   168
            QPointF offset = panGesture->offset();
hgs
parents:
diff changeset
   169
hgs
parents:
diff changeset
   170
            // Note: in horizontal orientation x should is treaten as Y, y as X.
hgs
parents:
diff changeset
   171
            QPointF movement;
hgs
parents:
diff changeset
   172
            if (mOrientation == Qt::Vertical) {
hgs
parents:
diff changeset
   173
                movement = offset;
hgs
parents:
diff changeset
   174
            }
hgs
parents:
diff changeset
   175
            else {
hgs
parents:
diff changeset
   176
                movement.setX(offset.y());
hgs
parents:
diff changeset
   177
                movement.setY(offset.x());
hgs
parents:
diff changeset
   178
            }
hgs
parents:
diff changeset
   179
hgs
parents:
diff changeset
   180
            // Gesture was long enough for place movement
hgs
parents:
diff changeset
   181
            if (qAbs(movement.x()) > (KCalenHScrollMoveParam * mContentWidth / 100)) {
hgs
parents:
diff changeset
   182
                if (movement.x() < 0) {
hgs
parents:
diff changeset
   183
                    mMoveDirection = ECalenScrollToNext;
hgs
parents:
diff changeset
   184
                    moveTo(QPointF((-mStartPosition.x() + mContentWidth),
hgs
parents:
diff changeset
   185
                        -mStartPosition.y()), KCalenScrollDaysTimeout);
hgs
parents:
diff changeset
   186
                }
hgs
parents:
diff changeset
   187
                else {
hgs
parents:
diff changeset
   188
                    mMoveDirection = ECalenScrollToPrev;
hgs
parents:
diff changeset
   189
                    moveTo(QPointF(-mStartPosition.x() - mContentWidth,
hgs
parents:
diff changeset
   190
                        -mStartPosition.y()), KCalenScrollDaysTimeout);
hgs
parents:
diff changeset
   191
                }
hgs
parents:
diff changeset
   192
            }
hgs
parents:
diff changeset
   193
            // Gesture was short one, reset to gesture start point
hgs
parents:
diff changeset
   194
            else {
hgs
parents:
diff changeset
   195
                qreal startPos = mStartPosition.x();
hgs
parents:
diff changeset
   196
                bool isNegative = false;
hgs
parents:
diff changeset
   197
                if (startPos < 0) {
hgs
parents:
diff changeset
   198
                    isNegative = true;
hgs
parents:
diff changeset
   199
                }
hgs
parents:
diff changeset
   200
                startPos = qAbs(startPos);
hgs
parents:
diff changeset
   201
                qreal normalizeValue = mContentWidth / 2;
hgs
parents:
diff changeset
   202
hgs
parents:
diff changeset
   203
                while (startPos > normalizeValue) {
hgs
parents:
diff changeset
   204
                    normalizeValue += mContentWidth;
hgs
parents:
diff changeset
   205
                }
hgs
parents:
diff changeset
   206
hgs
parents:
diff changeset
   207
                if (isNegative) {
hgs
parents:
diff changeset
   208
                    mStartPosition.setX(-(normalizeValue - (mContentWidth / 2)));
hgs
parents:
diff changeset
   209
                }
hgs
parents:
diff changeset
   210
                else {
hgs
parents:
diff changeset
   211
                    mStartPosition.setX(normalizeValue - (mContentWidth / 2));
hgs
parents:
diff changeset
   212
                }
hgs
parents:
diff changeset
   213
hgs
parents:
diff changeset
   214
                mMoveDirection = ECalenScrollNoDayChange;
hgs
parents:
diff changeset
   215
                moveTo(-mStartPosition, KCalenScrollDaysTimeout);
hgs
parents:
diff changeset
   216
            }
hgs
parents:
diff changeset
   217
        }
hgs
parents:
diff changeset
   218
    }
hgs
parents:
diff changeset
   219
    else {
hgs
parents:
diff changeset
   220
        HbScrollArea::gestureEvent(event);
hgs
parents:
diff changeset
   221
    }
hgs
parents:
diff changeset
   222
#else
hgs
parents:
diff changeset
   223
    // Let the content scroll area ignore pan gestures
hgs
parents:
diff changeset
   224
    if (QPanGesture *panGesture = qobject_cast<QPanGesture *> (event->gesture(
hgs
parents:
diff changeset
   225
                Qt::PanGesture))) {
hgs
parents:
diff changeset
   226
        // do nothing with pan gesture
hgs
parents:
diff changeset
   227
    }
hgs
parents:
diff changeset
   228
hgs
parents:
diff changeset
   229
    if (HbSwipeGesture *swipeGesture =
hgs
parents:
diff changeset
   230
        qobject_cast<HbSwipeGesture *> (event->gesture(Qt::SwipeGesture))) {
hgs
parents:
diff changeset
   231
        if (swipeGesture->state() == Qt::GestureStarted) {
hgs
parents:
diff changeset
   232
            mStartPosition = contentWidget()->pos();
hgs
parents:
diff changeset
   233
            
hgs
parents:
diff changeset
   234
            qreal swipeAngle = swipeGesture->sceneSwipeAngle();
57
hgs
parents: 55
diff changeset
   235
            if (isHorizontalSwipe(swipeAngle)) {
45
hgs
parents:
diff changeset
   236
                if (QSwipeGesture::Left == 
hgs
parents:
diff changeset
   237
                    swipeGesture->sceneHorizontalDirection()) {
hgs
parents:
diff changeset
   238
                    mMoveDirection = ECalenScrollToNext;
hgs
parents:
diff changeset
   239
                    moveTo(QPointF((-mStartPosition.x() + mContentWidth),
hgs
parents:
diff changeset
   240
                            -mStartPosition.y()), KCalenScrollDaysTimeout);
hgs
parents:
diff changeset
   241
                }
hgs
parents:
diff changeset
   242
                else if (QSwipeGesture::Right == 
hgs
parents:
diff changeset
   243
                    swipeGesture->sceneHorizontalDirection()) {
hgs
parents:
diff changeset
   244
                    mMoveDirection = ECalenScrollToPrev;
hgs
parents:
diff changeset
   245
                    moveTo(QPointF(-mStartPosition.x() - mContentWidth,
hgs
parents:
diff changeset
   246
                            -mStartPosition.y()), KCalenScrollDaysTimeout);
hgs
parents:
diff changeset
   247
                }
hgs
parents:
diff changeset
   248
            }
hgs
parents:
diff changeset
   249
        }
hgs
parents:
diff changeset
   250
    }
hgs
parents:
diff changeset
   251
#endif
hgs
parents:
diff changeset
   252
}
hgs
parents:
diff changeset
   253
hgs
parents:
diff changeset
   254
/*!
hgs
parents:
diff changeset
   255
 \brief Filters pan gesture events.
hgs
parents:
diff changeset
   256
 
hgs
parents:
diff changeset
   257
 Filters events if this object has been installed as an event filter for
hgs
parents:
diff changeset
   258
 the watched object. Handles horizontal pan gestures (ignores vertical).
hgs
parents:
diff changeset
   259
 When moving scroll area all gesture events are blocked.
hgs
parents:
diff changeset
   260
 
hgs
parents:
diff changeset
   261
 \param obj Watched object
hgs
parents:
diff changeset
   262
 \param event Event to be filtered
hgs
parents:
diff changeset
   263
 \return Returns TRUE if event was handled. FALSE otherwise.
hgs
parents:
diff changeset
   264
 */
hgs
parents:
diff changeset
   265
bool CalenDayContentScrollArea::eventFilter(QObject *obj, QEvent *event)
hgs
parents:
diff changeset
   266
{
hgs
parents:
diff changeset
   267
    Q_UNUSED(obj);
hgs
parents:
diff changeset
   268
hgs
parents:
diff changeset
   269
    bool handled = false;
hgs
parents:
diff changeset
   270
hgs
parents:
diff changeset
   271
    // Check if we get a gesture event
hgs
parents:
diff changeset
   272
    if (event->type() == QEvent::Gesture) {
hgs
parents:
diff changeset
   273
hgs
parents:
diff changeset
   274
        // Blocks handling of gesture events if scrolling started by 
hgs
parents:
diff changeset
   275
        // pan gesture is in progress
hgs
parents:
diff changeset
   276
        if (mIsMoving) {
hgs
parents:
diff changeset
   277
            handled = true;
hgs
parents:
diff changeset
   278
        }
hgs
parents:
diff changeset
   279
        else {
hgs
parents:
diff changeset
   280
            QGestureEvent* gesture = static_cast<QGestureEvent*> (event);
hgs
parents:
diff changeset
   281
hgs
parents:
diff changeset
   282
            // Check if we get a pan gesture
hgs
parents:
diff changeset
   283
            QPanGesture *panGesture = qobject_cast<QPanGesture*> (
hgs
parents:
diff changeset
   284
                gesture->gesture(Qt::PanGesture));
hgs
parents:
diff changeset
   285
            if (panGesture) {
hgs
parents:
diff changeset
   286
                checkPanDirection(panGesture);
hgs
parents:
diff changeset
   287
                if (mPanDayDirection == ECalenPanHorizontal) {
hgs
parents:
diff changeset
   288
                    gestureEvent(gesture);
hgs
parents:
diff changeset
   289
                    handled = true;
hgs
parents:
diff changeset
   290
                }
hgs
parents:
diff changeset
   291
            }
hgs
parents:
diff changeset
   292
        }
hgs
parents:
diff changeset
   293
    }
hgs
parents:
diff changeset
   294
hgs
parents:
diff changeset
   295
    return handled;
hgs
parents:
diff changeset
   296
}
hgs
parents:
diff changeset
   297
hgs
parents:
diff changeset
   298
/*!
hgs
parents:
diff changeset
   299
 \brief Overriden event handler.
hgs
parents:
diff changeset
   300
 
hgs
parents:
diff changeset
   301
 Handles events:
hgs
parents:
diff changeset
   302
 - gesture/focus events blocked when horizontal scrolling is in progress
hgs
parents:
diff changeset
   303
 - layout request event - scrolls to middle widget if current position is wrong
hgs
parents:
diff changeset
   304
 
hgs
parents:
diff changeset
   305
 \param e Event to be handled
hgs
parents:
diff changeset
   306
 \return Returns TRUE if event was handled. FALSE otherwise.
hgs
parents:
diff changeset
   307
 */
hgs
parents:
diff changeset
   308
bool CalenDayContentScrollArea::event(QEvent *e)
hgs
parents:
diff changeset
   309
{
hgs
parents:
diff changeset
   310
    bool result = false;
hgs
parents:
diff changeset
   311
hgs
parents:
diff changeset
   312
    // Blocks base class handler for certain events if scrolling started
hgs
parents:
diff changeset
   313
    // by pan gesture is in progress
hgs
parents:
diff changeset
   314
    if (mIsMoving && (e->type() == QEvent::Gesture || e->type()
hgs
parents:
diff changeset
   315
        == QEvent::GestureOverride || e->type() == QEvent::FocusOut
hgs
parents:
diff changeset
   316
        || e->type() == QEvent::FocusIn)) {
hgs
parents:
diff changeset
   317
        result = true;
hgs
parents:
diff changeset
   318
    }
hgs
parents:
diff changeset
   319
    if (!result) {
hgs
parents:
diff changeset
   320
        // Call base class handler
hgs
parents:
diff changeset
   321
        result = HbScrollArea::event(e);
hgs
parents:
diff changeset
   322
        
hgs
parents:
diff changeset
   323
        // Scroll to middle widget when layout request
64
1881ad52dc45 201033_03
hgs
parents: 63
diff changeset
   324
        if (e->type() == QEvent::LayoutRequest || e->type() == QEvent::Show) {
45
hgs
parents:
diff changeset
   325
            scrollToMiddleWidget();
hgs
parents:
diff changeset
   326
        }  
hgs
parents:
diff changeset
   327
    }
hgs
parents:
diff changeset
   328
hgs
parents:
diff changeset
   329
    return result;
hgs
parents:
diff changeset
   330
}
hgs
parents:
diff changeset
   331
hgs
parents:
diff changeset
   332
/*!
hgs
parents:
diff changeset
   333
 \brief Checks the direction of pan gesture.
hgs
parents:
diff changeset
   334
 
hgs
parents:
diff changeset
   335
 Changes the scrolling style according to movement direction, 
hgs
parents:
diff changeset
   336
 stores the orientation of the pan gesture.
hgs
parents:
diff changeset
   337
 Function is used when switching widgets by panning gesture is enabled.
hgs
parents:
diff changeset
   338
 
hgs
parents:
diff changeset
   339
 \param panGesture Pan gesture event
hgs
parents:
diff changeset
   340
 */
hgs
parents:
diff changeset
   341
void CalenDayContentScrollArea::checkPanDirection(QPanGesture *panGesture)
hgs
parents:
diff changeset
   342
{
hgs
parents:
diff changeset
   343
    // Gets the offset of pan gesture.
hgs
parents:
diff changeset
   344
    QPointF offset = panGesture->offset();
hgs
parents:
diff changeset
   345
hgs
parents:
diff changeset
   346
    // Note: in horizontal orientation x should is treaten as Y, y as X.
hgs
parents:
diff changeset
   347
    QPointF movement;
hgs
parents:
diff changeset
   348
    if (mOrientation == Qt::Vertical) {
hgs
parents:
diff changeset
   349
        movement = offset;
hgs
parents:
diff changeset
   350
    }
hgs
parents:
diff changeset
   351
    else {
hgs
parents:
diff changeset
   352
        movement.setX(offset.y());
hgs
parents:
diff changeset
   353
        movement.setY(offset.x());
hgs
parents:
diff changeset
   354
    }
hgs
parents:
diff changeset
   355
hgs
parents:
diff changeset
   356
    // If gesture is started check leading movement direction
hgs
parents:
diff changeset
   357
    if (panGesture->state() == Qt::GestureStarted) {
hgs
parents:
diff changeset
   358
        if (qAbs(movement.x()) < qAbs(movement.y())) {
hgs
parents:
diff changeset
   359
            mPanDayDirection = ECalenPanVertical;
hgs
parents:
diff changeset
   360
        }
hgs
parents:
diff changeset
   361
        else {
hgs
parents:
diff changeset
   362
            mStartPosition = contentWidget()->pos();
hgs
parents:
diff changeset
   363
            mPanDayDirection = ECalenPanHorizontal;
hgs
parents:
diff changeset
   364
        }
hgs
parents:
diff changeset
   365
    }
hgs
parents:
diff changeset
   366
}
hgs
parents:
diff changeset
   367
hgs
parents:
diff changeset
   368
/*!
hgs
parents:
diff changeset
   369
 \brief Scrolls the contents to the newPosition in a given time.
hgs
parents:
diff changeset
   370
 
hgs
parents:
diff changeset
   371
 Sets the flag to indicate that scrolling is in progress. Use this function
hgs
parents:
diff changeset
   372
 for scrolling with timeout > 0 to block gesture and focus events during
hgs
parents:
diff changeset
   373
 scroll area movement.
hgs
parents:
diff changeset
   374
 
hgs
parents:
diff changeset
   375
 \param newPosition Destination position
hgs
parents:
diff changeset
   376
 \param time Time of scroll movement
hgs
parents:
diff changeset
   377
 */
hgs
parents:
diff changeset
   378
void CalenDayContentScrollArea::moveTo(const QPointF &newPosition, int time)
hgs
parents:
diff changeset
   379
{
hgs
parents:
diff changeset
   380
    // Connect to scrollingEnded SIGNAL to get feedback when scrolling ends
hgs
parents:
diff changeset
   381
    connect(this, SIGNAL(scrollingEnded()), this, SLOT(moveFinished()));
hgs
parents:
diff changeset
   382
    
hgs
parents:
diff changeset
   383
    // Scroll the content to new position and set isMoving flag
hgs
parents:
diff changeset
   384
    scrollContentsTo(newPosition, time);
hgs
parents:
diff changeset
   385
    mIsMoving = true;
hgs
parents:
diff changeset
   386
    
hgs
parents:
diff changeset
   387
    // Emit signal that moving has just started
hgs
parents:
diff changeset
   388
    if (mMoveDirection != ECalenScrollNoDayChange) {
hgs
parents:
diff changeset
   389
        emit scrollAreaMoveStarted(mMoveDirection);
hgs
parents:
diff changeset
   390
    }
hgs
parents:
diff changeset
   391
}
hgs
parents:
diff changeset
   392
hgs
parents:
diff changeset
   393
/*!
57
hgs
parents: 55
diff changeset
   394
 \brief isHorizontalSwipe
hgs
parents: 55
diff changeset
   395
 
hgs
parents: 55
diff changeset
   396
 \return TRUE if horizontal swipe was recognized (angle in specific range)
hgs
parents: 55
diff changeset
   397
 */
hgs
parents: 55
diff changeset
   398
bool CalenDayContentScrollArea::isHorizontalSwipe(qreal angle) const
hgs
parents: 55
diff changeset
   399
{
hgs
parents: 55
diff changeset
   400
    bool isHSwipe = false;
hgs
parents: 55
diff changeset
   401
    if ((angle < KCalenSwipeAngle) || 
hgs
parents: 55
diff changeset
   402
        ((angle > 180 - KCalenSwipeAngle) && (angle < 180 + KCalenSwipeAngle)) ||
hgs
parents: 55
diff changeset
   403
        (angle > 360 - KCalenSwipeAngle)) {
hgs
parents: 55
diff changeset
   404
        isHSwipe = true;
hgs
parents: 55
diff changeset
   405
    }
hgs
parents: 55
diff changeset
   406
    
hgs
parents: 55
diff changeset
   407
    return isHSwipe;
hgs
parents: 55
diff changeset
   408
}
hgs
parents: 55
diff changeset
   409
hgs
parents: 55
diff changeset
   410
/*!
45
hgs
parents:
diff changeset
   411
 \brief Slot which is called when moving of scroll area is finished.
hgs
parents:
diff changeset
   412
 
hgs
parents:
diff changeset
   413
 Resets internal isMoving flag.
hgs
parents:
diff changeset
   414
 */
hgs
parents:
diff changeset
   415
void CalenDayContentScrollArea::moveFinished()
hgs
parents:
diff changeset
   416
{
hgs
parents:
diff changeset
   417
    // Disconnect from signal, move is finished now
hgs
parents:
diff changeset
   418
    disconnect(this, SIGNAL(scrollingEnded()), this, SLOT(moveFinished()));
hgs
parents:
diff changeset
   419
    mIsMoving = false;
hgs
parents:
diff changeset
   420
    
hgs
parents:
diff changeset
   421
    // Emit signal that moving has just finished and reset direction
hgs
parents:
diff changeset
   422
    if (mMoveDirection != ECalenScrollNoDayChange) {
hgs
parents:
diff changeset
   423
        emit scrollAreaMoveFinished(mMoveDirection);
hgs
parents:
diff changeset
   424
        mMoveDirection = ECalenScrollNoDayChange;
hgs
parents:
diff changeset
   425
    }
hgs
parents:
diff changeset
   426
}
hgs
parents:
diff changeset
   427
hgs
parents:
diff changeset
   428
/*!
hgs
parents:
diff changeset
   429
 \brief Slot which is called whenever the orientation of the device changes.
hgs
parents:
diff changeset
   430
 
hgs
parents:
diff changeset
   431
 Stores screen width and orientation in private members.
hgs
parents:
diff changeset
   432
 
hgs
parents:
diff changeset
   433
 \param orientation Current device orientation
hgs
parents:
diff changeset
   434
 */
hgs
parents:
diff changeset
   435
void CalenDayContentScrollArea::orientationChanged(Qt::Orientation orientation)
hgs
parents:
diff changeset
   436
{
hgs
parents:
diff changeset
   437
    // Update the width of content area
hgs
parents:
diff changeset
   438
    mContentWidth = CalenDayUtils::instance()->contentWidth();
hgs
parents:
diff changeset
   439
    mOrientation = orientation;
55
hgs
parents: 45
diff changeset
   440
hgs
parents: 45
diff changeset
   441
    // Fix the width of scroll area
hgs
parents: 45
diff changeset
   442
    setMinimumWidth(mContentWidth);
hgs
parents: 45
diff changeset
   443
    setMaximumWidth(mContentWidth);
hgs
parents: 45
diff changeset
   444
hgs
parents: 45
diff changeset
   445
    scrollToMiddleWidget();
hgs
parents: 45
diff changeset
   446
45
hgs
parents:
diff changeset
   447
    // Reset flag related to moving
hgs
parents:
diff changeset
   448
    mPanDayDirection = ECalenPanNotSet;
hgs
parents:
diff changeset
   449
    mMoveDirection = ECalenScrollNoDayChange;
hgs
parents:
diff changeset
   450
    mIsMoving = false;
hgs
parents:
diff changeset
   451
}
hgs
parents:
diff changeset
   452
hgs
parents:
diff changeset
   453
// End of File