screensaver/snsrplugins/snsrbigclockscreensaverplugin/snsrclockwidgets/src/snsroledtimelabel.cpp
changeset 97 66b5fe3c07fd
child 98 e6f74eb7f69f
equal deleted inserted replaced
95:32e56106abf2 97:66b5fe3c07fd
       
     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: Time label for oled digital clock.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "snsroledtimelabel.h"
       
    19 #include "snsrcolors.h"
       
    20 
       
    21 #include <hbevent.h>
       
    22 #include <QPainter>
       
    23 
       
    24 
       
    25 
       
    26 /*!
       
    27     \class SnsrOledTimeLabel
       
    28     \ingroup group_snsrbigclockscreensaverprovider
       
    29     \brief Screensaver oled digital clock's time label
       
    30  */
       
    31 
       
    32 /*!
       
    33     Constructs a new SnsrOledTimeLabel.
       
    34     \param parent Parent object.
       
    35  */
       
    36 
       
    37 SnsrOledTimeLabel::SnsrOledTimeLabel(QGraphicsItem *parent)
       
    38     : HbTextItem(parent)
       
    39 {
       
    40     setTextColor(SnsrColors::PowerSaveModeWidgetColor);
       
    41     
       
    42     setMinimumLines( 1 );
       
    43     setMaximumLines( 1 );
       
    44     setAlignment(Qt::AlignCenter);
       
    45     
       
    46     // as we do own painting, must disable the flag that is enabled
       
    47     // by default due to performance optimatizations.
       
    48     setFlag(QGraphicsItem::ItemHasNoContents,false);
       
    49 }
       
    50 
       
    51 SnsrOledTimeLabel::~SnsrOledTimeLabel()
       
    52 {
       
    53 }
       
    54 
       
    55 /*!
       
    56     \reimp
       
    57     We want to draw only the outlines of the time label text and thus
       
    58     text is drawn here with QPainterPath and pen (no brush) which seems to be the
       
    59     only way to accomplish this. 
       
    60     Base class's Paint method must not be called!
       
    61     We use base class only to get the bounding rect inside which to draw the text
       
    62     (boundingRect has been adjusted to current text, font, alignment etc.)
       
    63  */
       
    64 void SnsrOledTimeLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    65 {
       
    66     Q_UNUSED(option);
       
    67     Q_UNUSED(widget);
       
    68     
       
    69     // TODO: changes needed for mirrored layout and/or other than latin digits??
       
    70     
       
    71     painter->save();   //saves the painter state.
       
    72     
       
    73     QPainterPath path;
       
    74     
       
    75     QRectF boundingRect = this->boundingRect();   
       
    76     QString timeText = this->text();
       
    77     QFont font = this->font();
       
    78 
       
    79     // As painterPath takes the left end of the text's baseline as 
       
    80     // a parameter we must calculate that point from boundingRect's bottom left 
       
    81     // so that font's descent (and baseline (=1)) are taken into account. 
       
    82     // We want to center the text horizontally too. The logic
       
    83     // is basically the same as used inside the base class for drawing the text
       
    84     // within boundingRect.
       
    85     QPointF leftEndOfBaseline(0,0);  // for painterPath
       
    86     QFontMetrics fontMetrics(font);
       
    87     int descent = fontMetrics.descent();
       
    88     int width = fontMetrics.width(timeText);   
       
    89     leftEndOfBaseline.setY(boundingRect.bottomLeft().y() - descent -1);
       
    90     leftEndOfBaseline.setX((boundingRect.width() - width) / 2 ); // centerH
       
    91     
       
    92     path.addText(leftEndOfBaseline, font, timeText);  
       
    93     
       
    94     painter->setFont(font);
       
    95 
       
    96     QPen newPen = painter->pen();
       
    97     newPen.setWidth( 1 ); // TODO: is this width ok?
       
    98     newPen.setColor(this->textColor());
       
    99     painter->setPen(newPen);
       
   100     
       
   101     painter->setBrush(QBrush(Qt::NoBrush));
       
   102 
       
   103   //  painter->setLayoutDirection ( layoutDirection() ); // TODO: needed or not?
       
   104     
       
   105     painter->setRenderHint(QPainter::Antialiasing, true); // TODO: use or not?
       
   106     
       
   107     painter->drawPath(path);
       
   108     
       
   109     // Must restore the painter or the whole UI will be messed up!
       
   110     painter->restore();
       
   111 
       
   112 }
       
   113