File indexing completed on 2024-05-05 04:02:56

0001 /*
0002     SPDX-FileCopyrightText: 2010 Ni Hui <shuizhuyuanluo@126.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "piece.h"
0008 
0009 #include "gamescene.h"
0010 
0011 #include <QGraphicsLineItem>
0012 
0013 Piece::Piece( KGameGraphicsViewRenderer* renderer, int x, int y, int color, QGraphicsItem* parent )
0014 : KGameRenderedGraphicsObject(renderer,QStringLiteral( "BLOCK_%1" ).arg(QString::number(color)),parent),
0015 m_x(x),
0016 m_y(y),
0017 m_color(color),
0018 m_rightLine(new QGraphicsLineItem),
0019 m_bottomLine(new QGraphicsLineItem),
0020 m_highlighter(new KGameRenderedGraphicsObject(renderer,QStringLiteral( "HIGHLIGHT" )))
0021 {
0022     setAcceptHoverEvents( true );
0023     setAcceptedMouseButtons( Qt::LeftButton );
0024     m_highlighter->setAcceptHoverEvents( false );
0025     m_highlighter->setAcceptedMouseButtons( Qt::NoButton );
0026 }
0027 
0028 Piece::~Piece()
0029 {
0030     delete m_rightLine;
0031     delete m_bottomLine;
0032     delete m_highlighter;
0033 }
0034 
0035 QPainterPath Piece::shape() const
0036 {
0037     QPainterPath path;
0038     path.addRect( boundingRect() );
0039     return path;
0040 }
0041 
0042 void Piece::hoverEnterEvent( QGraphicsSceneHoverEvent* event )
0043 {
0044     Q_UNUSED(event)
0045     Q_EMIT pieceHovered( m_x, m_y );
0046 }
0047 
0048 void Piece::hoverLeaveEvent( QGraphicsSceneHoverEvent* event )
0049 {
0050     Q_UNUSED(event)
0051     Q_EMIT pieceUnhovered( m_x, m_y );
0052 }
0053 
0054 void Piece::mousePressEvent( QGraphicsSceneMouseEvent* event )
0055 {
0056     Q_UNUSED(event)
0057     Q_EMIT pieceClicked( m_x, m_y );
0058 }
0059 
0060 #include "moc_piece.cpp"