1 | /* |
2 | * Copyright (c) 2010 Ni Hui <shuizhuyuanluo@126.com> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | */ |
18 | |
19 | #include "piece.h" |
20 | |
21 | #include "gamescene.h" |
22 | |
23 | #include <QGraphicsLineItem> |
24 | |
25 | Piece::Piece( KGameRenderer* renderer, int x, int y, int color, QGraphicsItem* parent ) |
26 | : KGameRenderedObjectItem(renderer,QString(QLatin1String( "BLOCK_%1" )).arg(QString::number(color)),parent), |
27 | m_x(x), |
28 | m_y(y), |
29 | m_color(color), |
30 | m_rightLine(new QGraphicsLineItem), |
31 | m_bottomLine(new QGraphicsLineItem), |
32 | m_highlighter(new KGameRenderedObjectItem(renderer,QLatin1String( "HIGHLIGHT" ))) |
33 | { |
34 | setAcceptHoverEvents( true ); |
35 | setAcceptedMouseButtons( Qt::LeftButton ); |
36 | m_highlighter->setAcceptHoverEvents( false ); |
37 | m_highlighter->setAcceptedMouseButtons( 0 ); |
38 | } |
39 | |
40 | Piece::~Piece() |
41 | { |
42 | delete m_rightLine; |
43 | delete m_bottomLine; |
44 | delete m_highlighter; |
45 | } |
46 | |
47 | QPainterPath Piece::shape() const |
48 | { |
49 | QPainterPath path; |
50 | path.addRect( boundingRect() ); |
51 | return path; |
52 | } |
53 | |
54 | void Piece::hoverEnterEvent( QGraphicsSceneHoverEvent* event ) |
55 | { |
56 | Q_UNUSED(event) |
57 | emit pieceHovered( m_x, m_y ); |
58 | } |
59 | |
60 | void Piece::hoverLeaveEvent( QGraphicsSceneHoverEvent* event ) |
61 | { |
62 | Q_UNUSED(event) |
63 | emit pieceUnhovered( m_x, m_y ); |
64 | } |
65 | |
66 | void Piece::mousePressEvent( QGraphicsSceneMouseEvent* event ) |
67 | { |
68 | Q_UNUSED(event) |
69 | emit pieceClicked( m_x, m_y ); |
70 | } |
71 | |