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