calendarui/views/dayview/src/calendayhourelement.cpp
branchRCL_3
changeset 65 12af337248b1
equal deleted inserted replaced
60:96907930389d 65:12af337248b1
       
     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:  Day view control of calendar
       
    15  *
       
    16  */
       
    17 
       
    18 // System includes
       
    19 #include <QGraphicsLinearLayout>
       
    20 #include <QPainter>
       
    21 #include <HbTextItem>
       
    22 #include <HbStyle>
       
    23 #include <HbColorScheme>
       
    24 #include <HbFontSpec>
       
    25 #include <HbExtendedLocale>
       
    26 
       
    27 // User includes
       
    28 #include "calendayhourelement.h"
       
    29 #include "calendaycontainer.h"
       
    30 #include "calendayhourscrollarea.h"
       
    31 
       
    32 // Constants
       
    33 /*!
       
    34  \brief Time format is: [0-9][0-9]:[0-9][0-9]
       
    35  */
       
    36 const int KCalenTimeFormatLength = 5;
       
    37 
       
    38 /*!
       
    39  \class CalenDayHourElement
       
    40  \brief Hour element widget for Calendar's Day View
       
    41  */
       
    42 
       
    43 /*!
       
    44  \brief Constructor
       
    45  
       
    46  \param time Time assigned to widget
       
    47  \param parent Widget's parent
       
    48  */
       
    49 CalenDayHourElement::CalenDayHourElement(
       
    50     const QTime &time,
       
    51     QGraphicsItem *parent) :
       
    52     HbWidget(parent), mHour(time)
       
    53 {
       
    54     // Necessary when widget implements own paint method
       
    55     setFlag(QGraphicsItem::ItemHasNoContents, false);
       
    56 
       
    57     HbDeviceProfile deviceProfile;
       
    58     mUnitInPixels = deviceProfile.unitValue();
       
    59 
       
    60     // Initialize hour line color
       
    61     mHourLineColor = HbColorScheme::color(KCalenHourLineColor);
       
    62 
       
    63     // Create text items
       
    64     HbExtendedLocale systemLocale = HbExtendedLocale::system();
       
    65 
       
    66     // Get current time format and (if there's a need) separate time from am/pm text
       
    67     QChar timeSeparator = ' ';
       
    68     QStringList timeTextList = systemLocale.format(time,
       
    69         r_qtn_time_usual_with_zero).split(timeSeparator);
       
    70 
       
    71     // If needed, prepend '0' to get proper time format: [0-9][0-9]:[0-9][0-9]
       
    72     QString timeString = timeTextList[0];
       
    73     if (timeString.length() < KCalenTimeFormatLength) {
       
    74         timeString.prepend('0');
       
    75     }
       
    76 
       
    77     QString ampmString = "";
       
    78     if (timeTextList.count() > 1) {
       
    79         ampmString = timeTextList[1].toLower();
       
    80     }
       
    81 
       
    82     HbTextItem* timeTextItem = new HbTextItem(timeString, this);
       
    83     HbTextItem* ampmTextItem = new HbTextItem(ampmString, this);
       
    84 
       
    85     HbStyle::setItemName(timeTextItem, QLatin1String("time"));
       
    86     HbStyle::setItemName(ampmTextItem, QLatin1String("ampm"));
       
    87 
       
    88     // Parent container is needed to update widget's time
       
    89     mContainer = static_cast<CalenDayHourScrollArea*> (parent);
       
    90 }
       
    91 
       
    92 /*!
       
    93  \brief Destructor
       
    94  */
       
    95 CalenDayHourElement::~CalenDayHourElement()
       
    96 {
       
    97 
       
    98 }
       
    99 
       
   100 /*!
       
   101  \brief Customized paint() function
       
   102  
       
   103  \param painter Painter
       
   104  \param option Style option
       
   105  \param widget
       
   106  */
       
   107 void CalenDayHourElement::paint(
       
   108     QPainter *painter,
       
   109     const QStyleOptionGraphicsItem *option,
       
   110     QWidget *widget)
       
   111 {
       
   112     Q_UNUSED(widget);
       
   113 
       
   114     painter->save();
       
   115     
       
   116     QRectF drawArea = option->rect;
       
   117 
       
   118     // Draw full hour line
       
   119     QPen linePen = QPen(mHourLineColor, KCalenHourLineThickness * mUnitInPixels);
       
   120     painter->setPen(linePen);
       
   121     
       
   122     QLineF fullHourLine(drawArea.bottomLeft(), drawArea.bottomRight());
       
   123     painter->drawLine(fullHourLine);
       
   124 
       
   125     // Draw extra line on top for midnight
       
   126     if (mHour.hour() == 0) {
       
   127         fullHourLine = QLineF(drawArea.topLeft(), drawArea.topRight());
       
   128         painter->drawLine(fullHourLine);
       
   129     }
       
   130 
       
   131     QDateTime currentDateTime = QDateTime::currentDateTime();
       
   132 
       
   133     // Draw the time line in theme color
       
   134     if (mContainer) {
       
   135         QDateTime containersDateTime = mContainer->dateTime();
       
   136 
       
   137         if (currentDateTime.date() == containersDateTime.date()
       
   138             && currentDateTime.time().hour() == mHour.hour()) {
       
   139 
       
   140             qreal currentTimeY = drawArea.height()
       
   141                 * currentDateTime.time().minute() / 60;
       
   142 
       
   143             QColor color = HbColorScheme::color(KCalenTimeLineColor);
       
   144             painter->setPen(QPen(color, KCalenCurrentTimeLineThickness
       
   145                 * mUnitInPixels, Qt::SolidLine, Qt::FlatCap));
       
   146             QLineF currentTimeline(drawArea.left(), drawArea.top() + currentTimeY,
       
   147                     drawArea.right(), drawArea.top() + currentTimeY);
       
   148             painter->drawLine(currentTimeline);
       
   149         }
       
   150     }
       
   151 
       
   152     painter->restore();
       
   153 }
       
   154 
       
   155 /*!
       
   156  \brief Sets time for hour element.
       
   157  
       
   158  \param time Time to be set for hour element
       
   159  */
       
   160 void CalenDayHourElement::setTime(const QTime &time)
       
   161 {
       
   162     mHour = time;
       
   163 }
       
   164 
       
   165 /*!
       
   166  \brief Returns time of hour element.
       
   167  
       
   168  \return Time of hour element
       
   169  */
       
   170 QTime CalenDayHourElement::time() const
       
   171 {
       
   172     return mHour;
       
   173 }
       
   174 
       
   175 // End of File