~kubuntu-members/killbots/4.11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  Copyright 2007-2009  Parker Coates <coates@kde.org>
 *
 *  This file is part of Killbots.
 *
 *  Killbots is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Killbots is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Killbots. If not, see <http://www.gnu.org/licenses/>.
 */

#include "numericdisplayitem.h"

#include "renderer.h"

#include <QtGui/QFontMetrics>
#include <QtGui/QPainter>


Killbots::NumericDisplayItem::NumericDisplayItem( const QString & label, QGraphicsItem * parent )
  : QObject(),
    KGameRenderedItem( Renderer::self(), "status", parent ),
    m_label( label ),
    m_value( 0 ),
    m_digits( 3 )
{
	setShapeMode( QGraphicsPixmapItem::BoundingRectShape );
	setFont( QFont() );
	setRenderSize( preferredSize() );
}


Killbots::NumericDisplayItem::~NumericDisplayItem()
{
}


int Killbots::NumericDisplayItem::value() const
{
	return m_value;
}


QString Killbots::NumericDisplayItem::label() const
{
	return m_label;
}


int Killbots::NumericDisplayItem::digits() const
{
	return m_digits;
}


QFont Killbots::NumericDisplayItem::font() const
{
	return m_font;
}


QSize Killbots::NumericDisplayItem::preferredSize()
{
	QSize labelSize = QFontMetrics( m_font ).boundingRect( m_label ).size();
	QSize digitsSize = QFontMetrics( m_boldFont ).boundingRect( QString( m_digits + 1, '8' ) ).size();

	return QSize( labelSize.width() + digitsSize.width() + 2 * m_margin,
	              qMax( labelSize.height(), digitsSize.height() ) + 2 * m_margin
	            );
}


void Killbots::NumericDisplayItem::paint( QPainter * p, const QStyleOptionGraphicsItem * option, QWidget * widget )
{
	KGameRenderedItem::paint( p, option, widget );

	p->save();

	QRectF textRect = boundingRect().adjusted( m_margin, m_margin, -m_margin, -m_margin );
	p->setPen( Renderer::self()->textColor() );

	p->setFont( m_font );
	p->drawText( textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, m_label );

	p->setFont( m_boldFont );
	p->drawText( textRect, Qt::AlignRight | Qt::AlignVCenter | Qt::TextSingleLine, QString::number( m_value ) );

	p->restore();
}


void Killbots::NumericDisplayItem::setValue( int value )
{
	if ( value != m_value )
	{
		m_value = value;
		update();
	}
}


void Killbots::NumericDisplayItem::setLabel( const QString & label )
{
	if ( label != m_label )
	{
		m_label = label;
		update();
	}
}


void Killbots::NumericDisplayItem::setDigits( int digits )
{
	if ( digits != m_digits )
	{
		m_digits = digits;
	}
}


void Killbots::NumericDisplayItem::setFont( const QFont & font )
{
	m_font = font;
	m_boldFont = m_font;
	m_boldFont.setBold( true );

	m_margin = int( QFontMetrics( m_boldFont ).height() * 0.6 );
}