~ubuntu-branches/ubuntu/quantal/kdegames/quantal

« back to all changes in this revision

Viewing changes to konquest/mapitems.cc

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:50 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20111215141750-6tj6brf4azhrt915
Tags: 4:4.7.90-0ubuntu1
new upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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>
7
 
 
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.
12
 
 
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.
17
 
 
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.
21
 
 */
22
 
 
23
 
#include "mapitems.h"
24
 
#include "gamelogic.h"
25
 
 
26
 
#include <QGraphicsScene>
27
 
#include <QPainter>
28
 
#include <QBrush>
29
 
#include <QTimer>
30
 
 
31
 
#include <kiconloader.h>
32
 
#include <kglobalsettings.h>
33
 
#include <klocale.h>
34
 
#include <kdebug.h>
35
 
#include <kcolorscheme.h>
36
 
 
37
 
#include "mapscene.h"
38
 
#include "map.h"
39
 
 
40
 
 
41
 
/********************************
42
 
    PlanetItem
43
 
 *******************************/
44
 
 
45
 
PlanetItem::PlanetItem (MapScene *scene, Sector *sector, GameLogic *gamelogic)
46
 
    : QGraphicsObject(),
47
 
      m_scene(scene),
48
 
      m_sector(sector),
49
 
      m_gamelogic(gamelogic),
50
 
      m_hovered(false),
51
 
      m_selected(false),
52
 
      m_blinkState(false)
53
 
{
54
 
    if (m_sector->planet() != NULL) {
55
 
        m_lookName = QString("planet_%1").arg(m_sector->planet()->planetLook() + 1);
56
 
    }
57
 
    setAcceptsHoverEvents(true);
58
 
 
59
 
    m_blinkTimer = new QTimer(this);
60
 
    connect(m_blinkTimer, SIGNAL(timeout()), this, SLOT(blinkPlanet()));
61
 
    connect(m_sector,     SIGNAL(update()),  this, SLOT(updatePlanet()));
62
 
}
63
 
 
64
 
 
65
 
void PlanetItem::updatePlanet()
66
 
{
67
 
    Planet  *planet = m_sector->planet();
68
 
    if (planet != NULL) {
69
 
        m_lookName = QString("planet_%1").arg(planet->planetLook() + 1);
70
 
        update();
71
 
    }
72
 
}
73
 
 
74
 
 
75
 
QRectF PlanetItem::boundingRect() const
76
 
{
77
 
    qreal size = m_scene->getSectorSize();
78
 
    return QRectF(m_sector->coord().y() * size + m_scene->itemsHorizontalOffset(),
79
 
                  m_sector->coord().x() * size,
80
 
                  size,
81
 
                  size);
82
 
}
83
 
 
84
 
void PlanetItem::paint(QPainter *p, const QStyleOptionGraphicsItem * /*option*/,
85
 
                       QWidget * /*widget*/)
86
 
{
87
 
    // Display a frame around the planet
88
 
    if (!m_sector->planet()->player()->isNeutral()) {
89
 
        QBrush backBrush = p->brush();
90
 
 
91
 
        backBrush.setColor(m_sector->planet()->player()->color());
92
 
        backBrush.setStyle(Qt::SolidPattern);
93
 
 
94
 
        p->setOpacity(0.5);
95
 
        p->fillRect(boundingRect(), backBrush );
96
 
        p->setOpacity(1);
97
 
    }
98
 
 
99
 
    // Display the planet
100
 
    qreal sectorSize = m_scene->getSectorSize();
101
 
    QPointF sectorTopLeft(m_sector->coord().y() * sectorSize + m_scene->itemsHorizontalOffset(),
102
 
                          m_sector->coord().x() * sectorSize);
103
 
 
104
 
    QPixmap planetPix = renderPixmap(m_lookName, sectorSize, sectorSize);
105
 
    p->drawPixmap(sectorTopLeft, planetPix);
106
 
 
107
 
    if ( m_hovered || (m_selected && m_blinkState) ) {
108
 
        QBrush  backBrush = p->brush();
109
 
 
110
 
        backBrush.setColor(KColorScheme(QPalette::Active).background().color());
111
 
        backBrush.setStyle(Qt::SolidPattern);
112
 
 
113
 
        p->setOpacity(0.3);
114
 
        p->fillRect(boundingRect(), backBrush );
115
 
        p->setOpacity(1);
116
 
    }
117
 
 
118
 
    // Show the name of the planet (on top of bkgnd)
119
 
 
120
 
    QRectF TextRect(sectorTopLeft.x(), sectorTopLeft.y(), sectorSize, sectorSize);
121
 
 
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());
126
 
 
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())))
131
 
    {
132
 
        QString shipCount = QString::number(m_sector->planet()->ships());
133
 
 
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);
139
 
    }
140
 
}
141
 
 
142
 
QPixmap PlanetItem::renderPixmap( const QString& svgId, int width, int height ) const
143
 
{
144
 
    QPixmap pix;
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);
152
 
    }
153
 
 
154
 
    return pix;
155
 
}
156
 
 
157
 
 
158
 
void PlanetItem::hoverEnterEvent( QGraphicsSceneHoverEvent * /*event*/ )
159
 
{
160
 
    m_hovered = true;
161
 
 
162
 
    Planet  *planet = m_sector->planet();
163
 
    m_scene->displayPlanetInfo(planet);
164
 
 
165
 
    update();
166
 
}
167
 
 
168
 
void PlanetItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * /*event*/ )
169
 
{
170
 
    m_hovered = false;
171
 
    m_scene->displayPlanetInfo(NULL, QPoint());
172
 
 
173
 
    update();
174
 
}
175
 
 
176
 
 
177
 
void PlanetItem::mousePressEvent( QGraphicsSceneMouseEvent * /*event*/ )
178
 
{
179
 
    m_selected = true;
180
 
    m_blinkTimer->start(500);
181
 
    update();
182
 
 
183
 
    emit planetItemSelected(this);
184
 
}
185
 
 
186
 
void PlanetItem::select(  )
187
 
{
188
 
    m_selected = true;
189
 
    m_blinkTimer->start(500);
190
 
    update();
191
 
}
192
 
 
193
 
void PlanetItem::unselect() {
194
 
    m_blinkTimer->stop();
195
 
    m_blinkState = false;
196
 
    m_selected   = false;
197
 
 
198
 
    update();
199
 
}
200
 
 
201
 
void PlanetItem::blinkPlanet()
202
 
{
203
 
    m_blinkState = !m_blinkState;
204
 
 
205
 
    update();
206
 
}
207
 
 
208
 
 
209
 
/********************************
210
 
    PlanetInfoItem
211
 
 *******************************/
212
 
 
213
 
 
214
 
PlanetInfoItem::PlanetInfoItem (GameLogic *gamelogic)
215
 
  : QGraphicsItem(),
216
 
    m_gamelogic(gamelogic),
217
 
    m_textDoc(),
218
 
    m_planet(NULL)
219
 
{
220
 
}
221
 
 
222
 
void PlanetInfoItem::setPlanet (Planet *planet)
223
 
{
224
 
    m_planet = planet;
225
 
 
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())))
230
 
    {
231
 
        text += QString("<br />" + i18n("Owner: %1", planet->player()->coloredName())
232
 
          + (m_gamelogic->options().NeutralsShowShips || !planet->player()->isNeutral() ?
233
 
             QString("<br />"
234
 
             + i18n("Ships: %1", planet->ships() )) :
235
 
             "")
236
 
          + "<br />"
237
 
          + i18n("Production: %1", planet->production() )
238
 
          + "<br />"
239
 
          + i18n("Kill percent: %1", planet->killPercentage() ));
240
 
    }
241
 
    m_textDoc.setHtml(text);
242
 
}
243
 
 
244
 
 
245
 
QRectF PlanetInfoItem::boundingRect() const
246
 
{
247
 
    return QRectF(0, 0, m_textDoc.idealWidth(), m_textDoc.size().height());
248
 
}
249
 
 
250
 
void PlanetInfoItem::paint(QPainter *p,
251
 
                           const QStyleOptionGraphicsItem * /*option*/,
252
 
                           QWidget * /*widget*/)
253
 
{
254
 
    QBrush  brush = p->brush();
255
 
 
256
 
    brush.setColor(KColorScheme(QPalette::Active).background().color());
257
 
    brush.setStyle(Qt::SolidPattern);
258
 
 
259
 
    p->setOpacity(0.7);
260
 
    p->fillRect(QRectF(0, 0,
261
 
                       m_textDoc.idealWidth() + 1,
262
 
                       m_textDoc.size().height() + 1),
263
 
                brush);
264
 
    p->setOpacity(1.0);
265
 
 
266
 
    m_textDoc.drawContents(p);
267
 
}