2
Copyright Russell Steffen <rsteffen@bayarea.net>
3
Copyright Stephan Zehetner <s.zehetner@nevox.org>
4
Copyright 2008-2009 Dmitry Suzdalev <dimsuz@gmail.com>
5
Copyright Inge Wallin <inge@lysator.liu.se>
6
Copyright Pierre Ducroquet <pinaraf@gmail.com>
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
#include "gamelogic.h"
26
#include <QGraphicsScene>
31
#include <kiconloader.h>
32
#include <kglobalsettings.h>
35
#include <kcolorscheme.h>
41
/********************************
43
*******************************/
45
PlanetItem::PlanetItem (MapScene *scene, Sector *sector, GameLogic *gamelogic)
49
m_gamelogic(gamelogic),
54
if (m_sector->planet() != NULL) {
55
m_lookName = QString("planet_%1").arg(m_sector->planet()->planetLook() + 1);
57
setAcceptsHoverEvents(true);
59
m_blinkTimer = new QTimer(this);
60
connect(m_blinkTimer, SIGNAL(timeout()), this, SLOT(blinkPlanet()));
61
connect(m_sector, SIGNAL(update()), this, SLOT(updatePlanet()));
65
void PlanetItem::updatePlanet()
67
Planet *planet = m_sector->planet();
69
m_lookName = QString("planet_%1").arg(planet->planetLook() + 1);
75
QRectF PlanetItem::boundingRect() const
77
qreal size = m_scene->getSectorSize();
78
return QRectF(m_sector->coord().y() * size + m_scene->itemsHorizontalOffset(),
79
m_sector->coord().x() * size,
84
void PlanetItem::paint(QPainter *p, const QStyleOptionGraphicsItem * /*option*/,
87
// Display a frame around the planet
88
if (!m_sector->planet()->player()->isNeutral()) {
89
QBrush backBrush = p->brush();
91
backBrush.setColor(m_sector->planet()->player()->color());
92
backBrush.setStyle(Qt::SolidPattern);
95
p->fillRect(boundingRect(), backBrush );
100
qreal sectorSize = m_scene->getSectorSize();
101
QPointF sectorTopLeft(m_sector->coord().y() * sectorSize + m_scene->itemsHorizontalOffset(),
102
m_sector->coord().x() * sectorSize);
104
QPixmap planetPix = renderPixmap(m_lookName, sectorSize, sectorSize);
105
p->drawPixmap(sectorTopLeft, planetPix);
107
if ( m_hovered || (m_selected && m_blinkState) ) {
108
QBrush backBrush = p->brush();
110
backBrush.setColor(KColorScheme(QPalette::Active).background().color());
111
backBrush.setStyle(Qt::SolidPattern);
114
p->fillRect(boundingRect(), backBrush );
118
// Show the name of the planet (on top of bkgnd)
120
QRectF TextRect(sectorTopLeft.x(), sectorTopLeft.y(), sectorSize, sectorSize);
122
QPixmap nameBackgroundPix = renderPixmap("planet_name_background", sectorSize, sectorSize);
123
p->drawPixmap(TextRect.topLeft(), nameBackgroundPix);
124
p->setFont(QFont("Times", 16));
125
p->drawText(TextRect, m_sector->planet()->name());
127
// Show the number of ships on the planet.
128
if((m_gamelogic->options().NeutralsShowShips || !m_sector->planet()->player()->isNeutral())
129
&& ((!m_gamelogic->options().BlindMap || m_gamelogic->currentPlayer() == m_sector->planet()->player())
130
|| (m_gamelogic->options().NeutralsShowShips && m_sector->planet()->player()->isNeutral())))
132
QString shipCount = QString::number(m_sector->planet()->ships());
134
QPixmap shipsBackgroundPix = renderPixmap("planet_ship_count_background",
135
sectorSize, sectorSize);
136
p->drawPixmap(TextRect.topLeft(), shipsBackgroundPix);
137
p->setFont(QFont("Times", 16));
138
p->drawText(TextRect, Qt::AlignRight | Qt::AlignBottom, shipCount);
142
QPixmap PlanetItem::renderPixmap( const QString& svgId, int width, int height ) const
145
QString cacheKey = QString("%1%2x%3").arg(svgId).arg(width).arg(height);
146
if (!m_scene->pixmapCache()->find(cacheKey, pix)) {
147
pix = QPixmap(width, height);
148
pix.fill(Qt::transparent);
149
QPainter pixPainter(&pix);
150
m_scene->renderer()->render(&pixPainter, svgId, QRect(0, 0, width, height));
151
m_scene->pixmapCache()->insert(cacheKey, pix);
158
void PlanetItem::hoverEnterEvent( QGraphicsSceneHoverEvent * /*event*/ )
162
Planet *planet = m_sector->planet();
163
m_scene->displayPlanetInfo(planet);
168
void PlanetItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * /*event*/ )
171
m_scene->displayPlanetInfo(NULL, QPoint());
177
void PlanetItem::mousePressEvent( QGraphicsSceneMouseEvent * /*event*/ )
180
m_blinkTimer->start(500);
183
emit planetItemSelected(this);
186
void PlanetItem::select( )
189
m_blinkTimer->start(500);
193
void PlanetItem::unselect() {
194
m_blinkTimer->stop();
195
m_blinkState = false;
201
void PlanetItem::blinkPlanet()
203
m_blinkState = !m_blinkState;
209
/********************************
211
*******************************/
214
PlanetInfoItem::PlanetInfoItem (GameLogic *gamelogic)
216
m_gamelogic(gamelogic),
222
void PlanetInfoItem::setPlanet (Planet *planet)
226
QString text = i18n("Planet name: %1", planet->name());
227
if((m_gamelogic->options().NeutralsShowStats || !planet->player()->isNeutral())
228
&& ((!m_gamelogic->options().BlindMap || m_gamelogic->currentPlayer() == planet->player())
229
|| (m_gamelogic->options().NeutralsShowStats && planet->player()->isNeutral())))
231
text += QString("<br />" + i18n("Owner: %1", planet->player()->coloredName())
232
+ (m_gamelogic->options().NeutralsShowShips || !planet->player()->isNeutral() ?
234
+ i18n("Ships: %1", planet->ships() )) :
237
+ i18n("Production: %1", planet->production() )
239
+ i18n("Kill percent: %1", planet->killPercentage() ));
241
m_textDoc.setHtml(text);
245
QRectF PlanetInfoItem::boundingRect() const
247
return QRectF(0, 0, m_textDoc.idealWidth(), m_textDoc.size().height());
250
void PlanetInfoItem::paint(QPainter *p,
251
const QStyleOptionGraphicsItem * /*option*/,
252
QWidget * /*widget*/)
254
QBrush brush = p->brush();
256
brush.setColor(KColorScheme(QPalette::Active).background().color());
257
brush.setStyle(Qt::SolidPattern);
260
p->fillRect(QRectF(0, 0,
261
m_textDoc.idealWidth() + 1,
262
m_textDoc.size().height() + 1),
266
m_textDoc.drawContents(p);