1 | /* |
2 | * Copyright 2007-2009 Parker Coates <coates@kde.org> |
3 | * |
4 | * This file is part of Killbots. |
5 | * |
6 | * Killbots is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * Killbots is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with Killbots. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "numericdisplayitem.h" |
21 | |
22 | #include "renderer.h" |
23 | |
24 | #include <QtGui/QFontMetrics> |
25 | #include <QtGui/QPainter> |
26 | |
27 | |
28 | Killbots::NumericDisplayItem::NumericDisplayItem( const QString & label, QGraphicsItem * parent ) |
29 | : QObject(), |
30 | KGameRenderedItem( Renderer::self(), "status" , parent ), |
31 | m_label( label ), |
32 | m_value( 0 ), |
33 | m_digits( 3 ) |
34 | { |
35 | setShapeMode( QGraphicsPixmapItem::BoundingRectShape ); |
36 | setFont( QFont() ); |
37 | setRenderSize( preferredSize() ); |
38 | } |
39 | |
40 | |
41 | Killbots::NumericDisplayItem::~NumericDisplayItem() |
42 | { |
43 | } |
44 | |
45 | |
46 | int Killbots::NumericDisplayItem::value() const |
47 | { |
48 | return m_value; |
49 | } |
50 | |
51 | |
52 | QString Killbots::NumericDisplayItem::label() const |
53 | { |
54 | return m_label; |
55 | } |
56 | |
57 | |
58 | int Killbots::NumericDisplayItem::digits() const |
59 | { |
60 | return m_digits; |
61 | } |
62 | |
63 | |
64 | QFont Killbots::NumericDisplayItem::font() const |
65 | { |
66 | return m_font; |
67 | } |
68 | |
69 | |
70 | QSize Killbots::NumericDisplayItem::preferredSize() |
71 | { |
72 | QSize labelSize = QFontMetrics( m_font ).boundingRect( m_label ).size(); |
73 | QSize digitsSize = QFontMetrics( m_boldFont ).boundingRect( QString( m_digits + 1, '8' ) ).size(); |
74 | |
75 | return QSize( labelSize.width() + digitsSize.width() + 2 * m_margin, |
76 | qMax( labelSize.height(), digitsSize.height() ) + 2 * m_margin |
77 | ); |
78 | } |
79 | |
80 | |
81 | void Killbots::NumericDisplayItem::paint( QPainter * p, const QStyleOptionGraphicsItem * option, QWidget * widget ) |
82 | { |
83 | KGameRenderedItem::paint( p, option, widget ); |
84 | |
85 | p->save(); |
86 | |
87 | QRectF textRect = boundingRect().adjusted( m_margin, m_margin, -m_margin, -m_margin ); |
88 | p->setPen( Renderer::self()->textColor() ); |
89 | |
90 | p->setFont( m_font ); |
91 | p->drawText( textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, m_label ); |
92 | |
93 | p->setFont( m_boldFont ); |
94 | p->drawText( textRect, Qt::AlignRight | Qt::AlignVCenter | Qt::TextSingleLine, QString::number( m_value ) ); |
95 | |
96 | p->restore(); |
97 | } |
98 | |
99 | |
100 | void Killbots::NumericDisplayItem::setValue( int value ) |
101 | { |
102 | if ( value != m_value ) |
103 | { |
104 | m_value = value; |
105 | update(); |
106 | } |
107 | } |
108 | |
109 | |
110 | void Killbots::NumericDisplayItem::setLabel( const QString & label ) |
111 | { |
112 | if ( label != m_label ) |
113 | { |
114 | m_label = label; |
115 | update(); |
116 | } |
117 | } |
118 | |
119 | |
120 | void Killbots::NumericDisplayItem::setDigits( int digits ) |
121 | { |
122 | if ( digits != m_digits ) |
123 | { |
124 | m_digits = digits; |
125 | } |
126 | } |
127 | |
128 | |
129 | void Killbots::NumericDisplayItem::setFont( const QFont & font ) |
130 | { |
131 | m_font = font; |
132 | m_boldFont = m_font; |
133 | m_boldFont.setBold( true ); |
134 | |
135 | m_margin = int( QFontMetrics( m_boldFont ).height() * 0.6 ); |
136 | } |
137 | |
138 | |
139 | |