File indexing completed on 2024-11-24 03:43:18

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 "mark.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 Mark::Mark(Scene* scene, const Move& move, bool animate, qreal fill) : QGraphicsSvgItem(),
0039   m_scene(scene), m_row(move.y()), m_col(move.x()), m_fill(fill) {
0040     setElementId(QString(move.player() == X ? QStringLiteral("x%1") : QStringLiteral("o%1"))
0041             .arg(QString::number(QRandomGenerator::global()->bounded(5) + 1)));
0042     m_tick = 20;
0043     m_ticker = nullptr;
0044     if (animate) {
0045         m_ticker = new QTimer(this);
0046         m_opacity = 0.0;
0047         connect(m_ticker, &QTimer::timeout, this, &Mark::tick);
0048         m_ticker->start(30);
0049     } else {
0050         m_opacity = 1.0;
0051     }
0052 
0053     setPos(m_scene->cellCenter(m_col, m_row));
0054 }
0055 
0056 Mark::~Mark() {
0057     if (m_ticker) {
0058         disconnect(m_ticker, &QTimer::timeout, this, &Mark::tick);
0059         m_ticker->stop();
0060         m_ticker->deleteLater();
0061     }
0062 }
0063 
0064 QRectF Mark::boundingRect() const {
0065     qreal width = m_scene->squareSize();
0066     qreal height = width;
0067     qreal margin = (1.0-m_fill) * width / 2.0;
0068     return { -width / 2.0  + margin,
0069                 -height / 2.0 + margin,
0070                 width  - 2.0*margin,
0071                 height - 2.0*margin};
0072 }
0073 
0074 void Mark::killAnimation() {
0075     if (m_ticker != nullptr) {
0076         m_ticker->stop();
0077         disconnect(m_ticker, nullptr, this, nullptr);
0078         m_ticker->deleteLater();
0079         m_ticker = nullptr;
0080         m_opacity = 1.0;
0081         update();
0082     }
0083 }
0084 
0085 void Mark::kill() {
0086     if (m_ticker) {
0087         disconnect(m_ticker, &QTimer::timeout, this, &Mark::tick);
0088         m_ticker->stop();
0089     } else {
0090         m_ticker = new QTimer(this);
0091     }
0092     connect(m_ticker, &QTimer::timeout, this, &Mark::killTick);
0093     m_ticker->start(30);
0094 }
0095 
0096 void Mark::killTick() {
0097     m_opacity -= 0.1;
0098     update();
0099     if (m_opacity <= 0.1) {
0100         m_ticker->stop();
0101         Q_EMIT killed(this);
0102     }
0103 }
0104 
0105 void Mark::tick() {
0106     --m_tick;
0107     if (m_tick == 0) {
0108         killAnimation();
0109     } else {
0110         m_opacity += 0.1;
0111         update();
0112     }
0113 }
0114 
0115 void Mark::paint(QPainter *p, const QStyleOptionGraphicsItem*, QWidget*) {
0116     p->setOpacity(m_opacity);
0117     renderer()->render(p, elementId(), boundingRect());
0118 }
0119 
0120 void Mark::setFill(qreal fill) {
0121     if (m_fill != fill) {
0122         m_fill = fill;
0123         prepareGeometryChange();
0124     }
0125 }
0126 
0127 usi Mark::col() const {
0128     return m_col;
0129 }
0130 
0131 usi Mark::row() const {
0132     return m_row;
0133 }
0134 
0135 } /* namespace gui */
0136 
0137 #include "moc_mark.cpp"