SVN_SILENT made messages (.desktop file)
[kdegames.git] / bovo / gui / mark.cc
blobceb443097207e899cfb3eba8d005c8eb842adcc7
1 /*******************************************************************
3 * This file is part of the KDE project "Bovo"
5 * Bovo is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * Bovo is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Bovo; see the file COPYING. If not, write to
17 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
20 ********************************************************************/
22 #include "mark.h"
24 #include <QtCore/QTimer>
25 #include <QtGui/QColor>
26 #include <QtGui/QPainter>
27 #include <QtSvg/QSvgRenderer>
29 #include "common.h"
30 #include "coord.h"
31 #include "move.h"
32 #include "scene.h"
34 using namespace bovo;
36 namespace gui {
38 Mark::Mark(Scene* scene, const Move& move, bool animate, qreal fill) : QGraphicsSvgItem(),
39 m_scene(scene), m_row(move.y()), m_col(move.x()), m_fill(fill) {
40 m_sizeShrink = 1.0/12.0; //1.0/(qrand()%5+7.0);
41 setElementId(QString(move.player() == X ? "x%1" : "o%1")
42 .arg(QString::number(qrand() % 5 + 1)));
43 m_tick = 20;
44 m_ticker = 0;
45 if (animate) {
46 m_ticker = new QTimer(this);
47 m_opacity = 0.0;
48 connect(m_ticker, SIGNAL(timeout()), this, SLOT(tick()));
49 m_ticker->start(30);
50 } else {
51 m_opacity = 1.0;
54 setPos(m_scene->cellCenter(m_col, m_row));
57 Mark::~Mark() {
58 if (m_ticker) {
59 disconnect(m_ticker, SIGNAL(timeout()), this, SLOT(tick()));
60 m_ticker->stop();
61 m_ticker->deleteLater();
65 QRectF Mark::boundingRect() const {
66 qreal width = m_scene->squareSize();
67 qreal height = width;
68 qreal margin = (1.0-m_fill) * width / 2.0;
69 return QRectF( -width / 2.0 + margin,
70 -height / 2.0 + margin,
71 width - 2.0*margin,
72 height - 2.0*margin);
75 void Mark::killAnimation() {
76 if (m_ticker != 0) {
77 m_ticker->stop();
78 disconnect(m_ticker, 0, this, 0);
79 m_ticker->deleteLater();
80 m_ticker = 0;
81 m_opacity = 1.0;
82 update();
86 void Mark::kill() {
87 if (m_ticker) {
88 disconnect(m_ticker, SIGNAL(timeout()), this, SLOT(tick()));
89 m_ticker->stop();
90 } else {
91 m_ticker = new QTimer(this);
93 connect(m_ticker, SIGNAL(timeout()), this, SLOT(killTick()));
94 m_ticker->start(30);
97 void Mark::killTick() {
98 m_opacity -= 0.1;
99 update();
100 if (m_opacity <= 0.1) {
101 m_ticker->stop();
102 emit killed(this);
106 void Mark::tick() {
107 --m_tick;
108 if (m_tick == 0) {
109 killAnimation();
110 } else {
111 m_opacity += 0.1;
112 update();
116 void Mark::paint(QPainter *p, const QStyleOptionGraphicsItem*, QWidget*) {
117 p->setOpacity(m_opacity);
118 renderer()->render(p, elementId(), boundingRect());
121 void Mark::setFill(qreal fill) {
122 m_fill = fill;
123 prepareGeometryChange();
126 usi Mark::col() const {
127 return m_col;
130 usi Mark::row() const {
131 return m_row;
134 } /* namespace gui */
136 #include "mark.moc"