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"
24 #include <QtGui/QFontMetrics>
25 #include <QtGui/QPainter>
28 Killbots::GameStatusDisplayItem::GameStatusDisplayItem( const QString
& labelText
, QGraphicsItem
* parent
)
29 : QGraphicsItem( parent
),
35 setSize( preferredSize() );
39 Killbots::GameStatusDisplayItem::~GameStatusDisplayItem()
44 void Killbots::GameStatusDisplayItem::setSize( QSize size
)
46 prepareGeometryChange();
47 m_boundingRect
= QRectF( QPointF(), size
);
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
)
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() );
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
) );
91 void Killbots::GameStatusDisplayItem::setText( const QString
& text
)
93 if ( text
!= m_label
)
101 QString
Killbots::GameStatusDisplayItem::text() const
107 void Killbots::GameStatusDisplayItem::setValue( int value
)
109 if ( value
!= m_value
)
117 int Killbots::GameStatusDisplayItem::value() const
123 void Killbots::GameStatusDisplayItem::setDigits( int digits
)
125 if ( digits
!= m_digits
)
132 int Killbots::GameStatusDisplayItem::digits() const
138 void Killbots::GameStatusDisplayItem::setFont( const QFont
& font
)
140 if ( font
!= m_font
)
145 m_boldFont
.setBold( true );
147 m_margin
= int( m_boldFont
.pixelSize() * 0.6 );
152 QFont
Killbots::GameStatusDisplayItem::font() const