File indexing completed on 2025-04-27 03:48:09
0001 /******************************************************************* 0002 * 0003 * Copyright 2007 Aron Boström <c02ab@efd.lth.se> 0004 * 0005 * Bovo is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation; either version 2, or (at your option) 0008 * any later version. 0009 * 0010 * Bovo is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with Bovo; see the file COPYING. If not, write to 0017 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 * 0020 ********************************************************************/ 0021 0022 #include "hintitem.h" 0023 0024 #include <QTimer> 0025 #include <QPainter> 0026 #include <QSvgRenderer> 0027 #include <QRandomGenerator> 0028 0029 #include "common.h" 0030 #include "coord.h" 0031 #include "move.h" 0032 #include "scene.h" 0033 0034 using namespace bovo; 0035 0036 namespace gui { 0037 0038 HintItem::HintItem(Scene* scene, const Move& hint, bool animate, qreal fill) 0039 : QGraphicsSvgItem(), m_scene(scene), m_row(hint.y()), 0040 m_col(hint.x()), m_fill(fill) { 0041 setElementId(QString(hint.player() == X ? QStringLiteral("x%1") : QStringLiteral("o%1")) 0042 .arg(QString::number(QRandomGenerator::global()->bounded(5) + 1))); 0043 m_tick = 16; 0044 m_tickUp = true; 0045 m_ticker = nullptr; 0046 if (animate) { 0047 m_ticker = new QTimer(this); 0048 m_opacity = 0.0; 0049 connect(m_ticker, &QTimer::timeout, this, &HintItem::tick); 0050 m_ticker->start(30); 0051 } else { 0052 m_opacity = 0.4; 0053 } 0054 0055 setPos(m_scene->cellCenter(m_col, m_row)); 0056 } 0057 0058 HintItem::~HintItem() { 0059 if (m_ticker) { 0060 disconnect(m_ticker, nullptr, this, nullptr); 0061 m_ticker->stop(); 0062 m_ticker->deleteLater(); 0063 } 0064 } 0065 0066 QRectF HintItem::boundingRect() const { 0067 qreal width = m_scene->squareSize(); 0068 qreal height = width; 0069 qreal margin = (1.0-m_fill) * width / 2.0; 0070 return { -width / 2.0 + margin, 0071 -height / 2.0 + margin, 0072 width - 2.0*margin, 0073 height - 2.0*margin}; 0074 } 0075 0076 0077 void HintItem::killAnimation() { 0078 if (m_ticker) { 0079 m_ticker->stop(); 0080 disconnect(m_ticker, nullptr, this, nullptr); 0081 m_opacity = 0.4; 0082 update(); 0083 } 0084 } 0085 0086 void HintItem::kill() { 0087 connect(m_ticker, &QTimer::timeout, this, &HintItem::killTick); 0088 m_ticker->start(); 0089 } 0090 0091 void HintItem::killTick() { 0092 m_opacity -= 0.05; 0093 update(); 0094 if (m_opacity <= 0.05) { 0095 m_ticker->stop(); 0096 Q_EMIT killed(); 0097 } 0098 } 0099 0100 void HintItem::tick() { 0101 --m_tick; 0102 if (m_tick == 0) { 0103 killAnimation(); 0104 } else { 0105 if (m_tickUp && m_tick > 5) { 0106 m_opacity += 0.1; 0107 } else if (m_tickUp) { 0108 m_opacity -= 0.1; 0109 m_tickUp = false; 0110 } else { 0111 m_opacity -= 0.1; 0112 } 0113 update(); 0114 } 0115 } 0116 0117 // HintItem::setEnabled(enabled) { 0118 // m_enabled = enabled; 0119 // } 0120 0121 void HintItem::paint(QPainter *p, const QStyleOptionGraphicsItem*, QWidget*) { 0122 p->setOpacity(m_opacity); 0123 renderer()->render(p, elementId(), boundingRect()); 0124 } 0125 0126 void HintItem::setFill(qreal fill) { 0127 m_fill = fill; 0128 } 0129 0130 } /* namespace gui */ 0131 0132 #include "moc_hintitem.cpp"