SVN_SILENT made messages (.desktop file)
[kdegames.git] / killbots / gamestatusdisplayitem.cpp
blob27798d831d9088a05bfac8e0f46ee1672500d9ed
1 /*
2 * Copyright 2007-2009 Parker Coates <parker.coates@gmail.com>
4 * This file is part of Killbots.
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.
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.
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/>.
20 #include "gamestatusdisplayitem.h"
22 #include "render.h"
24 #include <QtGui/QFontMetrics>
25 #include <QtGui/QPainter>
28 Killbots::GameStatusDisplayItem::GameStatusDisplayItem( const QString & labelText, QGraphicsItem * parent )
29 : QGraphicsItem( parent ),
30 m_label( labelText ),
31 m_value( 0 ),
32 m_digits( 3 )
34 setFont( QFont() );
35 setSize( preferredSize() );
39 Killbots::GameStatusDisplayItem::~GameStatusDisplayItem()
44 void Killbots::GameStatusDisplayItem::setSize( QSize size )
46 prepareGeometryChange();
47 m_boundingRect = QRectF( QPointF(), size );
48 update();
52 QSize Killbots::GameStatusDisplayItem::preferredSize()
54 QSize labelSize = QFontMetrics( m_font ).boundingRect( m_label ).size();
55 QSize digitsSize = QFontMetrics( m_boldFont ).boundingRect( QString( m_digits + 1, '8' ) ).size();
57 return QSize( labelSize.width() + digitsSize.width() + 2 * m_margin,
58 qMax( labelSize.height(), digitsSize.height() ) + 2 * m_margin
63 QRectF Killbots::GameStatusDisplayItem::boundingRect() const
65 return m_boundingRect;
69 void Killbots::GameStatusDisplayItem::paint( QPainter * p, const QStyleOptionGraphicsItem * option, QWidget * widget )
71 Q_UNUSED( option )
72 Q_UNUSED( widget )
74 p->save();
76 p->drawPixmap( boundingRect().topLeft(), Render::renderElement( "status", boundingRect().size().toSize() ) );
78 QRectF textRect = boundingRect().adjusted( m_margin, m_margin, -m_margin, -m_margin );
79 p->setPen( Render::textColor() );
81 p->setFont( m_font );
82 p->drawText( textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, m_label );
84 p->setFont( m_boldFont );
85 p->drawText( textRect, Qt::AlignRight | Qt::AlignVCenter | Qt::TextSingleLine, QString::number( m_value ) );
87 p->restore();
91 void Killbots::GameStatusDisplayItem::setText( const QString & text )
93 if ( text != m_label )
95 m_label = text;
96 update();
101 QString Killbots::GameStatusDisplayItem::text() const
103 return m_label;
107 void Killbots::GameStatusDisplayItem::setValue( int value )
109 if ( value != m_value )
111 m_value = value;
112 update();
117 int Killbots::GameStatusDisplayItem::value() const
119 return m_value;
123 void Killbots::GameStatusDisplayItem::setDigits( int digits )
125 if ( digits != m_digits )
127 m_digits = digits;
132 int Killbots::GameStatusDisplayItem::digits() const
134 return m_digits;
138 void Killbots::GameStatusDisplayItem::setFont( const QFont & font )
140 if ( font != m_font )
142 m_font = font;
144 m_boldFont = font;
145 m_boldFont.setBold( true );
147 m_margin = int( m_boldFont.pixelSize() * 0.6 );
152 QFont Killbots::GameStatusDisplayItem::font() const
154 return m_font;