calendarui/views/dayview/src/calendayutils.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:  CalenDayUtils utility class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <hbdeviceprofile.h>
       
    20 #include <hbstyle.h>
       
    21 #include <hbinstance.h>
       
    22 #include <qdatetime.h>
       
    23 
       
    24 // User includes
       
    25 #include "calendayutils.h"
       
    26 #include "calendaycommonheaders.h"
       
    27 #include "agendaentry.h"
       
    28 #include "calendateutils.h"
       
    29 
       
    30 // Initialization of static member
       
    31 CalenDayUtils* CalenDayUtils::mInstance = 0;
       
    32 
       
    33 /*!
       
    34  \class CalenDayUtils
       
    35  \brief Singleton utility class.
       
    36  
       
    37  Class cannot be used in console applications.
       
    38  
       
    39  Provided functionalities (getters):
       
    40  - screen width of device
       
    41  - default width of hour element
       
    42  - default height of hour element
       
    43  - default width of content area
       
    44  - current orientation of screen
       
    45  - pointer to the main window of application
       
    46  */
       
    47 
       
    48 /*!
       
    49  \brief Returns the instance of CalenDayUtils class
       
    50  */
       
    51 CalenDayUtils *CalenDayUtils::instance()
       
    52 {
       
    53     if (!mInstance) {
       
    54         mInstance = new CalenDayUtils();
       
    55     }
       
    56     return mInstance;
       
    57 }
       
    58 
       
    59 /*!
       
    60  \brief Destructor
       
    61  */
       
    62 CalenDayUtils::~CalenDayUtils()
       
    63 {
       
    64     
       
    65 }
       
    66 
       
    67 /*!
       
    68  \brief screenWidth
       
    69  
       
    70  \return Width of main window's screen
       
    71  */
       
    72 qreal CalenDayUtils::screenWidth() const
       
    73 {
       
    74     ASSERT(mMainWindow);
       
    75     
       
    76     return mMainWindow->layoutRect().width();
       
    77 }
       
    78 
       
    79 /*!
       
    80  \brief hourElementWidth
       
    81  
       
    82  \return Width of hour element
       
    83  */
       
    84 qreal CalenDayUtils::hourElementWidth() const
       
    85 {
       
    86     return mHourElementWidth;
       
    87 }
       
    88 
       
    89 /*!
       
    90  \brief hourElementHeight
       
    91  
       
    92  \return Height of hour element
       
    93  */
       
    94 qreal CalenDayUtils::hourElementHeight() const
       
    95 {
       
    96     return mHourElementHeight;
       
    97 }
       
    98 
       
    99 /*!
       
   100  \brief contentWidth
       
   101  
       
   102  \return Width of content area
       
   103  */
       
   104 qreal CalenDayUtils::contentWidth() const
       
   105 {
       
   106     return (screenWidth() - mHourElementWidth);
       
   107 }
       
   108 
       
   109 /*!
       
   110  \brief orientation
       
   111  
       
   112  \return Orientation of main window
       
   113  */
       
   114 Qt::Orientation CalenDayUtils::orientation() const
       
   115 {
       
   116     ASSERT(mMainWindow);
       
   117         
       
   118     return mMainWindow->orientation();
       
   119 }
       
   120 
       
   121 /*!
       
   122  \brief mainWindow
       
   123  
       
   124  \return Pointer to main window of application
       
   125  */
       
   126 HbMainWindow* CalenDayUtils::mainWindow()
       
   127 {
       
   128     ASSERT(mMainWindow);
       
   129     
       
   130     return mMainWindow;
       
   131 }
       
   132 
       
   133 /*!
       
   134  \brief isHorizontalSwipe
       
   135  
       
   136  \return TRUE if horizontal swipe was recognized (angle in specific range)
       
   137  */
       
   138 bool CalenDayUtils::isHorizontalSwipe(qreal angle) const
       
   139 {
       
   140     bool isHSwipe = false;
       
   141     if ((angle < KCalenSwipeAngle) || 
       
   142         ((angle > 180 - KCalenSwipeAngle) && (angle < 180 + KCalenSwipeAngle)) ||
       
   143         (angle > 360 - KCalenSwipeAngle)) {
       
   144         isHSwipe = true;
       
   145     }
       
   146     
       
   147     return isHSwipe;
       
   148 }
       
   149 
       
   150 /*!
       
   151  \brief getEventValidStartEndTime
       
   152  \brief Get event's valid start/end time from agenda entry.
       
   153  */
       
   154 void CalenDayUtils::getEventValidStartEndTime( QDateTime& start, QDateTime& end, 
       
   155                                     AgendaEntry& entry, QDateTime& currentDate )
       
   156 {
       
   157     start = entry.startTime();
       
   158     end = entry.endTime();
       
   159     
       
   160     if ( !CalenDateUtils::onSameDay( start, currentDate ) ) {
       
   161         start = CalenDateUtils::beginningOfDay( currentDate );
       
   162     }
       
   163     
       
   164     if ( !CalenDateUtils::onSameDay( end, currentDate ) ) {
       
   165        QDateTime tommorrow( currentDate.addDays( 1 ));
       
   166        end = CalenDateUtils::beginningOfDay( tommorrow ).addSecs( -60 );
       
   167     }
       
   168 }
       
   169 
       
   170 /*!
       
   171  \brief Constructor
       
   172  */
       
   173 CalenDayUtils::CalenDayUtils() : mMainWindow(NULL)
       
   174 {
       
   175     if (HbInstance::instance()->allMainWindows().count() > 0) {
       
   176         mMainWindow = HbInstance::instance()->allMainWindows().first();
       
   177     }
       
   178     mHourElementWidth = calculateHourElementWidth();
       
   179     mHourElementHeight = calculateHourElementHeight();
       
   180 }
       
   181 
       
   182 /*!
       
   183  \brief Calculates the width of hour element according to UI spec.
       
   184  
       
   185  \return Calculated width of hour element
       
   186  */
       
   187 qreal CalenDayUtils::calculateHourElementWidth() const
       
   188 {
       
   189     HbStyle style;
       
   190     HbDeviceProfile deviceProfile;
       
   191     qreal unitInPixels = deviceProfile.unitValue();
       
   192 
       
   193     // Calculate element's preferred width
       
   194     qreal prefWidth = 0.0;
       
   195     qreal textWidth = 0.0;
       
   196     qreal horizontalSpacing = 0.0;
       
   197     
       
   198     textWidth = 8.04 * unitInPixels; // pix (according to UI spec)   
       
   199     style.parameter(QString("hb-param-margin-gene-middle-horizontal"), 
       
   200         horizontalSpacing, deviceProfile);
       
   201     prefWidth = horizontalSpacing * 2 + textWidth;
       
   202     
       
   203     return prefWidth;
       
   204 }
       
   205 
       
   206 /*!
       
   207  \brief Calculates the height of hour element according to UI spec.
       
   208  
       
   209  \return Calculated height of hour element
       
   210  */
       
   211 qreal CalenDayUtils::calculateHourElementHeight() const
       
   212 {
       
   213     HbStyle style;
       
   214     HbDeviceProfile deviceProfile;
       
   215     
       
   216     qreal unitInPixels = deviceProfile.unitValue();
       
   217     
       
   218     // Calculate element's preferred height
       
   219     qreal prefHeight = 0.0;
       
   220     qreal textHeight = 0.0;
       
   221     qreal verticalSpacing = 0.0;
       
   222         
       
   223     qreal bottomSpacer = 3.0 * unitInPixels;
       
   224     style.parameter(QString("hb-param-text-height-secondary"), textHeight, 
       
   225         deviceProfile);
       
   226     style.parameter(QString("hb-param-margin-gene-middle-vertical"), 
       
   227         verticalSpacing, deviceProfile);
       
   228 
       
   229     prefHeight = textHeight * 2; //time + ampm
       
   230     prefHeight += verticalSpacing * 2;
       
   231     prefHeight += bottomSpacer;
       
   232     
       
   233     return prefHeight;
       
   234 }