SVN_SILENT made messages (.desktop file)
[kdegames.git] / bovo / gui / hintitem.cc
blob96c3bcaa0dd2f8242731a256bf8adc03e06bcf1c
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 "hintitem.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 HintItem::HintItem(Scene* scene, const Move& hint, bool animate, qreal fill)
39 : QGraphicsSvgItem(), m_scene(scene), m_row(hint.y()),
40 m_col(hint.x()), m_fill(fill) {
41 m_sizeShrink = 1.0/(qrand()%5+7.0);
42 setElementId(QString(hint.player() == X ? "x%1" : "o%1")
43 .arg(QString::number(qrand() % 5 + 1)));
44 m_tick = 16;
45 m_tickUp = true;
46 m_ticker = 0;
47 if (animate) {
48 m_ticker = new QTimer(this);
49 m_opacity = 0.0;
50 connect(m_ticker, SIGNAL(timeout()), this, SLOT(tick()));
51 m_ticker->start(30);
52 } else {
53 m_opacity = 0.4;
56 setPos(m_scene->cellCenter(m_col, m_row));
59 HintItem::~HintItem() {
60 if (m_ticker) {
61 disconnect(m_ticker, 0, this, 0);
62 m_ticker->stop();
63 m_ticker->deleteLater();
67 QRectF HintItem::boundingRect() const {
68 qreal width = m_scene->squareSize();
69 qreal height = width;
70 qreal margin = (1.0-m_fill) * width / 2.0;
71 return QRectF( -width / 2.0 + margin,
72 -height / 2.0 + margin,
73 width - 2.0*margin,
74 height - 2.0*margin);
78 void HintItem::killAnimation() {
79 if (m_ticker) {
80 m_ticker->stop();
81 disconnect(m_ticker, 0, this, 0);
82 m_opacity = 0.4;
83 update();
87 void HintItem::kill() {
88 connect(m_ticker, SIGNAL(timeout()), this, SLOT(killTick()));
89 m_ticker->start();
92 void HintItem::killTick() {
93 m_opacity -= 0.05;
94 update();
95 if (m_opacity <= 0.05) {
96 m_ticker->stop();
97 emit killed();
101 void HintItem::tick() {
102 --m_tick;
103 if (m_tick == 0) {
104 killAnimation();
105 } else {
106 if (m_tickUp && m_tick > 5) {
107 m_opacity += 0.1;
108 } else if (m_tickUp) {
109 m_opacity -= 0.1;
110 m_tickUp = false;
111 } else {
112 m_opacity -= 0.1;
114 update();
118 // HintItem::setEnabled(enabled) {
119 // m_enabled = enabled;
120 // }
122 void HintItem::paint(QPainter *p, const QStyleOptionGraphicsItem*, QWidget*) {
123 p->setOpacity(m_opacity);
124 renderer()->render(p, elementId(), boundingRect());
127 void HintItem::setFill(qreal fill) {
128 m_fill = fill;
131 } /* namespace gui */
133 #include "hintitem.moc"