calendarui/views/dayview/src/calendayhourscrollarea.cpp
changeset 45 b6db4fd4947b
child 55 2c54b51f39c4
child 58 ef813d54df51
equal deleted inserted replaced
23:fd30d51f876b 45:b6db4fd4947b
       
     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:  CalenDayHourScrollArea implementation.
       
    15  *
       
    16  */
       
    17 
       
    18 // System includes
       
    19 #include <QGraphicsLinearLayout>
       
    20 
       
    21 // User includes
       
    22 #include "calendayhourscrollarea.h"
       
    23 #include "calendayhourelement.h"
       
    24 #include "calendayutils.h"
       
    25 
       
    26 /*!
       
    27  \class CalenDayHourScrollArea
       
    28  \brief Scrollable container class for hour elements.
       
    29  
       
    30  It handles vertical scrolling.
       
    31  */
       
    32 
       
    33 /*!
       
    34  \brief Constructor
       
    35  
       
    36  Configures scroll area settings and resets internal stares of widget.
       
    37  Gets the width of device.
       
    38  
       
    39  \param parent The parent of scroll area widget
       
    40  */
       
    41 CalenDayHourScrollArea::CalenDayHourScrollArea(QGraphicsItem *parent) :
       
    42     HbScrollArea(parent),
       
    43     mDateTime(QDateTime())
       
    44 {
       
    45     // Set scroll settings
       
    46     setScrollDirections(Qt::Vertical);
       
    47     setScrollingStyle(HbScrollArea::PanWithFollowOn);
       
    48     setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
       
    49     grabGesture(Qt::PanGesture, Qt::ReceivePartialGestures);
       
    50     
       
    51     // Get height and width of hour elements and set fixed size policy
       
    52     qreal hourElementHeight = CalenDayUtils::instance()->hourElementHeight();
       
    53     qreal hourElementWidth = CalenDayUtils::instance()->hourElementWidth();
       
    54 
       
    55     // Create widget for hour elements
       
    56     HbWidget *hourWidget = new HbWidget();
       
    57 
       
    58     // Create and insert hour elements into vertical layout
       
    59     QGraphicsLinearLayout* hourLayout = new QGraphicsLinearLayout(Qt::Vertical,
       
    60         NULL);
       
    61     for (int i = 0; i < 24; i++) {
       
    62         CalenDayHourElement* element = new CalenDayHourElement(QTime(i, 0),
       
    63             this);
       
    64         element->setPreferredWidth(hourElementWidth);
       
    65         element->setPreferredHeight(hourElementHeight);
       
    66         element->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, 
       
    67             QSizePolicy::Fixed));
       
    68         hourLayout->addItem(element);
       
    69         
       
    70         mHourElements.append(element);
       
    71     }
       
    72     
       
    73     hourLayout->setContentsMargins(0.0, 0.0, 0.0, 0.0);
       
    74     hourLayout->setSpacing(0.0);
       
    75     
       
    76     // Fix the size of scroll area
       
    77     setMinimumWidth(hourElementWidth);
       
    78     setMaximumWidth(hourElementWidth);
       
    79 
       
    80     // Apply hour layout for new widget and set content widget to scroll area
       
    81     hourWidget->setLayout(hourLayout);
       
    82     setContentWidget(hourWidget);
       
    83 }
       
    84 
       
    85 /*!
       
    86  \brief Destructor
       
    87  */
       
    88 CalenDayHourScrollArea::~CalenDayHourScrollArea()
       
    89 {
       
    90 }
       
    91 
       
    92 /*!
       
    93  \brief SLOT scrolls the view vertically to new position.
       
    94  
       
    95  \param newPosition New position
       
    96  */
       
    97 void CalenDayHourScrollArea::scrollVertically(const QPointF &newPosition)
       
    98 {
       
    99     QPointF currentPos = contentWidget()->pos();
       
   100     if (abs(newPosition.y()) != abs(currentPos.y())) {
       
   101         currentPos.setY(newPosition.y());
       
   102         scrollContentsTo(currentPos, 0);
       
   103     }
       
   104 }
       
   105 
       
   106 /*!
       
   107  \brief Sets date and time for this container.  
       
   108  
       
   109  \param dateTime new date and time
       
   110  */
       
   111 void CalenDayHourScrollArea::setDateTime(const QDateTime &dateTime)
       
   112 {
       
   113     mDateTime = dateTime;
       
   114 }
       
   115 /*!
       
   116  \brief Returns date and time assigned to current view.
       
   117 */
       
   118 QDateTime CalenDayHourScrollArea::dateTime() const
       
   119 {
       
   120     return mDateTime;
       
   121 }
       
   122 
       
   123 /*!
       
   124  \brief Scroll view to given hour.
       
   125  
       
   126   \param  An hour In 24 hour format (0 - 23)
       
   127 */
       
   128 void CalenDayHourScrollArea::scrollToHour(int hour)
       
   129 {
       
   130     CalenDayHourElement *hourElement = mHourElements.at(hour); 
       
   131     QRectF hourElementRect = hourElement->rect();
       
   132     hourElementRect = hourElement->mapRectToParent(hourElementRect);
       
   133     QPointF newPos = hourElementRect.topLeft();
       
   134     
       
   135     //Ensure that we won't scroll out of bound
       
   136     CalenDayHourElement *lastElement = mHourElements.last();
       
   137     qreal bottomEdge = lastElement->mapRectToParent(lastElement->rect()).bottom();
       
   138     qreal viewHeight = rect().height();
       
   139 
       
   140     if(bottomEdge - newPos.y() < viewHeight){
       
   141         newPos.setY(bottomEdge - viewHeight);
       
   142     }
       
   143     
       
   144     scrollContentsTo(newPos);
       
   145 }
       
   146 
       
   147 // End of File