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