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