1 |
torben |
1 |
/*************************************************************************** |
2 |
|
|
* Copyright (C) 2005 by Torben Nielsen * |
3 |
|
|
* torben@t-hoerup.dk * |
4 |
|
|
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the * |
17 |
|
|
* Free Software Foundation, Inc., * |
18 |
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
#include <qstring.h> |
21 |
|
|
#include <qpainter.h> |
22 |
|
|
#include <qpopupmenu.h> |
23 |
|
|
|
24 |
|
|
#include "board.h" |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
Board::Board(QWidget *parent, const char *name) |
28 |
|
|
: QWidget(parent, name) |
29 |
|
|
{ |
30 |
torben |
4 |
m_sol = NULL; |
31 |
torben |
1 |
} |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
Board::~Board() |
35 |
|
|
{ |
36 |
torben |
4 |
if (m_sol != NULL) |
37 |
|
|
delete m_sol; |
38 |
torben |
1 |
} |
39 |
|
|
|
40 |
torben |
4 |
void Board::setMatrix(Solution* sol) |
41 |
torben |
1 |
{ |
42 |
torben |
6 |
|
43 |
|
|
if (m_sol != NULL) |
44 |
|
|
{ |
45 |
|
|
delete m_sol; |
46 |
|
|
m_sol = NULL; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
if (sol != NULL) |
50 |
torben |
4 |
m_sol = sol->copy(); |
51 |
|
|
|
52 |
torben |
1 |
this->repaint(true); |
53 |
|
|
} |
54 |
|
|
|
55 |
torben |
5 |
|
56 |
torben |
1 |
|
57 |
|
|
void Board::paintEvent( QPaintEvent *event ) |
58 |
|
|
{ |
59 |
|
|
QPainter p (this); |
60 |
|
|
for (int i=0;i<(m_size+2);i++) { |
61 |
|
|
p.drawLine(0, i*30, m_size*30, i*30); |
62 |
|
|
p.drawLine(i*30, 0, i*30, m_size*30); |
63 |
|
|
} |
64 |
|
|
|
65 |
torben |
4 |
if (m_sol != NULL) { |
66 |
torben |
1 |
QBrush brush( black); |
67 |
|
|
p.setBrush( brush ); |
68 |
|
|
for (int i=0; i<m_size; i++) |
69 |
|
|
for (int j=0; j<m_size; j++) { |
70 |
torben |
4 |
if (m_sol->getMatrix(i,j)) |
71 |
torben |
1 |
p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360); |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
void Board::mousePressEvent( QMouseEvent *event) |
77 |
|
|
{ |
78 |
|
|
if (event->button() == LeftButton) { |
79 |
|
|
this->repaint(true); |
80 |
|
|
QPainter p( this ); |
81 |
|
|
QPen bluePen( blue ); |
82 |
|
|
p.setPen( bluePen ); |
83 |
|
|
int x = event->x() / 30; |
84 |
|
|
int y = event->y() / 30; |
85 |
|
|
|
86 |
|
|
p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) ); |
87 |
|
|
p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) ); |
88 |
|
|
|
89 |
|
|
QPen redPen( red ); |
90 |
|
|
p.setPen( redPen ); |
91 |
|
|
|
92 |
|
|
int startx,stopx; |
93 |
|
|
int starty,stopy; |
94 |
|
|
starty = stopy = y; |
95 |
|
|
startx = stopx = x; |
96 |
|
|
|
97 |
torben |
4 |
// f�rste skr� linie |
98 |
torben |
1 |
while (startx > 0 && starty > 0) { |
99 |
|
|
startx--; |
100 |
|
|
starty--; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
while (stopx < (m_size-1) && stopy < (m_size-1) ) { |
104 |
|
|
stopx++; |
105 |
|
|
stopy++; |
106 |
|
|
} |
107 |
|
|
p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25)); |
108 |
|
|
|
109 |
torben |
4 |
//n�ste skr� linie |
110 |
torben |
1 |
startx = stopx = x; |
111 |
|
|
starty = stopy = y; |
112 |
|
|
while (startx > 0 && starty < (m_size-1) ) { |
113 |
|
|
startx--; |
114 |
|
|
starty++; |
115 |
|
|
} |
116 |
|
|
while (stopx < (m_size-1) && stopy >0) { |
117 |
|
|
stopx++; |
118 |
|
|
stopy--; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
p.drawLine(QPoint( (startx*30)+5, (starty*30)+25), QPoint( (stopx*30)+25, (stopy*30)+5)); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
void Board::setSize( int size ) |
126 |
|
|
{ |
127 |
torben |
4 |
if (m_sol != NULL) |
128 |
|
|
{ |
129 |
|
|
delete m_sol; |
130 |
|
|
m_sol = NULL; |
131 |
|
|
} |
132 |
torben |
6 |
|
133 |
torben |
1 |
m_size = size; |
134 |
|
|
this->setMaximumSize( (size * 30)+1, (size * 30)+1 ); |
135 |
|
|
this->setMinimumSize( size * 30, size * 30); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
void Board::contextMenuEvent( QContextMenuEvent *event) |
139 |
|
|
{ |
140 |
torben |
4 |
if (m_sol == NULL) |
141 |
|
|
return; |
142 |
|
|
|
143 |
torben |
1 |
QPopupMenu *contextMenu = new QPopupMenu( this ); |
144 |
|
|
contextMenu->setCheckable( false ); |
145 |
|
|
contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) ); |
146 |
|
|
contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) ); |
147 |
|
|
contextMenu->insertItem("Vertical mirror", this, SLOT ( mirrorV() ) ); |
148 |
|
|
contextMenu->insertItem("Horisontal mirror", this, SLOT( mirrorH() ) ); |
149 |
|
|
|
150 |
|
|
contextMenu->exec( event->globalPos() ); |
151 |
|
|
delete contextMenu; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
void Board::rotateLeft() |
155 |
|
|
{ |
156 |
torben |
4 |
m_sol->rotate90(); |
157 |
torben |
1 |
repaint( true ); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
void Board::rotateRight() |
161 |
|
|
{ |
162 |
torben |
4 |
m_sol->rotate90(); |
163 |
|
|
m_sol->rotate90(); |
164 |
|
|
m_sol->rotate90(); |
165 |
torben |
1 |
repaint( true ); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
void Board::mirrorV() |
169 |
|
|
{ |
170 |
torben |
4 |
m_sol->mirror(); |
171 |
torben |
1 |
repaint( true ); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
void Board::mirrorH() |
175 |
|
|
{ |
176 |
torben |
4 |
m_sol->mirror(); |
177 |
|
|
m_sol->rotate90(); |
178 |
|
|
m_sol->rotate90(); |
179 |
torben |
1 |
repaint( true ); |
180 |
|
|
} |