-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.cpp
166 lines (145 loc) · 3 KB
/
Piece.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Mise en oeuvre de Piece.h
*
* @file Piece.cxx
*/
// A besoin de la declaration de la classe
#include "Piece.h"
#include<iostream>
using namespace std;
Piece::Piece()
{
this->setFixedSize(Echiquier::SIZE,Echiquier::SIZE);
cout << "Une piece creee: par defaut" << endl;
// ne fait rien => objet instancie mais non valide.
m_selected = false;
setCursor(Qt::OpenHandCursor);
}
//Constructeur avec parametre
Piece::Piece(int x, int y, bool white, Echiquier *parent):
QWidget(parent)
{
this->setFixedSize(Echiquier::SIZE,Echiquier::SIZE);
cout << "Une piece creee: avec parametre" << endl;
m_x = x;
m_y = y;
m_white = white;
m_selected = false;
movePiece(x,y);
setCursor(Qt::OpenHandCursor);
}
//Constructeur par copie
Piece::Piece(const Piece &autre)
{
this->setFixedSize(Echiquier::SIZE, Echiquier::SIZE);
this->setGeometry(autre.geometry());
m_x = autre.m_x;
m_y = autre.m_y;
m_white = autre.m_white;
m_selected = autre.m_selected;
setCursor(Qt::OpenHandCursor);
}
Piece::~Piece()
{
//cout << "Une piece detruite" << endl;
}
void
Piece::movePiece( int x, int y )
{
int size = Echiquier::SIZE;
this->move((x-1)*size,(y-1)*size);
m_x = x;
m_y = y;
}
int
Piece::x()
{
return m_x;
}
int
Piece::y()
{
return m_y;
}
bool
Piece::isWhite()
{
return m_white;
}
void
Piece::setSelected(bool select){
m_selected = select;
}
bool
Piece::mouvementLegal( Echiquier & e, int x, int y ) {
if (e.getPiece(x,y) == NULL) {
if(mouvementValide(e,x,y)){
return true;
}
}
else if ( e.getPiece(x,y)->isWhite() != this-> isWhite() ) {
if(mouvementValide(e,x,y)) {
return true;
}
}
return false;
}
bool
Piece::mouvementValide( Echiquier & e, int x, int y )
{
}
void
Piece::paintEvent(QPaintEvent *)
{
}
void
Piece::paintPiece(QString name)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
int size = Echiquier::SIZE;
if(m_selected){
QColor color(162,107,205);
painter.fillRect(1,1,size-1,size-1,color);
}
QImage img;
//img.load(name);
img.load(":/Images/" + name);
if(img.isNull()) {
painter.drawText(120,20,"Image");
}
else {
QPixmap pix;
pix = QPixmap::fromImage(img);
if(pix.isNull()) {
painter.drawText(70,20,"Pixmap");
}
else {
painter.drawPixmap(0,0,size,size,pix);
}
}
painter.end();
}
void
Piece::mousePressEvent ( QMouseEvent *e )
{
// Store the click point
m_clicked = true;
m_mousePos = e->pos();
setCursor(Qt::ClosedHandCursor);
emit clickPiece();
}
void
Piece::mouseReleaseEvent ( QMouseEvent *e )
{
// check if cursor not moved since click beginning
int size = Echiquier::SIZE;
QRect rect = QRect(0,0,size,size);
if ((m_clicked) && (rect.contains(e->pos())))
{
// emit release signal
emit releasePiece();
}
m_clicked = false;
setCursor(Qt::OpenHandCursor);
}