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 |
m_sol = NULL; |
31 |
} |
32 |
|
33 |
|
34 |
Board::~Board() |
35 |
{ |
36 |
if (m_sol != NULL) |
37 |
delete m_sol; |
38 |
} |
39 |
|
40 |
void Board::setMatrix(Solution* sol) |
41 |
{ |
42 |
if (sol != NULL) |
43 |
m_sol = sol->copy(); |
44 |
|
45 |
this->repaint(true); |
46 |
} |
47 |
|
48 |
/* |
49 |
void Board::setMatrix(IntSolution sol) |
50 |
{ |
51 |
#warning need a closer look |
52 |
|
53 |
m_hasData = true; |
54 |
m_sol = Solution(); |
55 |
for (int i=0;i<m_size;i++) |
56 |
m_sol.matrix[i][ sol.imatrix[i] ] = true; |
57 |
this->repaint(true); |
58 |
|
59 |
}*/ |
60 |
|
61 |
|
62 |
void Board::paintEvent( QPaintEvent *event ) |
63 |
{ |
64 |
QPainter p (this); |
65 |
for (int i=0;i<(m_size+2);i++) { |
66 |
p.drawLine(0, i*30, m_size*30, i*30); |
67 |
p.drawLine(i*30, 0, i*30, m_size*30); |
68 |
} |
69 |
|
70 |
if (m_sol != NULL) { |
71 |
QBrush brush( black); |
72 |
p.setBrush( brush ); |
73 |
for (int i=0; i<m_size; i++) |
74 |
for (int j=0; j<m_size; j++) { |
75 |
if (m_sol->getMatrix(i,j)) |
76 |
p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360); |
77 |
} |
78 |
} |
79 |
} |
80 |
|
81 |
void Board::mousePressEvent( QMouseEvent *event) |
82 |
{ |
83 |
if (event->button() == LeftButton) { |
84 |
this->repaint(true); |
85 |
QPainter p( this ); |
86 |
QPen bluePen( blue ); |
87 |
p.setPen( bluePen ); |
88 |
int x = event->x() / 30; |
89 |
int y = event->y() / 30; |
90 |
|
91 |
p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) ); |
92 |
p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) ); |
93 |
|
94 |
QPen redPen( red ); |
95 |
p.setPen( redPen ); |
96 |
|
97 |
int startx,stopx; |
98 |
int starty,stopy; |
99 |
starty = stopy = y; |
100 |
startx = stopx = x; |
101 |
|
102 |
// f�rste skr� linie |
103 |
while (startx > 0 && starty > 0) { |
104 |
startx--; |
105 |
starty--; |
106 |
} |
107 |
|
108 |
while (stopx < (m_size-1) && stopy < (m_size-1) ) { |
109 |
stopx++; |
110 |
stopy++; |
111 |
} |
112 |
p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25)); |
113 |
|
114 |
//n�ste skr� linie |
115 |
startx = stopx = x; |
116 |
starty = stopy = y; |
117 |
while (startx > 0 && starty < (m_size-1) ) { |
118 |
startx--; |
119 |
starty++; |
120 |
} |
121 |
while (stopx < (m_size-1) && stopy >0) { |
122 |
stopx++; |
123 |
stopy--; |
124 |
} |
125 |
|
126 |
p.drawLine(QPoint( (startx*30)+5, (starty*30)+25), QPoint( (stopx*30)+25, (stopy*30)+5)); |
127 |
} |
128 |
} |
129 |
|
130 |
void Board::setSize( int size ) |
131 |
{ |
132 |
if (m_sol != NULL) |
133 |
{ |
134 |
delete m_sol; |
135 |
m_sol = NULL; |
136 |
} |
137 |
m_size = size; |
138 |
this->setMaximumSize( (size * 30)+1, (size * 30)+1 ); |
139 |
this->setMinimumSize( size * 30, size * 30); |
140 |
} |
141 |
|
142 |
void Board::contextMenuEvent( QContextMenuEvent *event) |
143 |
{ |
144 |
if (m_sol == NULL) |
145 |
return; |
146 |
|
147 |
QPopupMenu *contextMenu = new QPopupMenu( this ); |
148 |
contextMenu->setCheckable( false ); |
149 |
contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) ); |
150 |
contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) ); |
151 |
contextMenu->insertItem("Vertical mirror", this, SLOT ( mirrorV() ) ); |
152 |
contextMenu->insertItem("Horisontal mirror", this, SLOT( mirrorH() ) ); |
153 |
|
154 |
contextMenu->exec( event->globalPos() ); |
155 |
delete contextMenu; |
156 |
} |
157 |
|
158 |
void Board::rotateLeft() |
159 |
{ |
160 |
m_sol->rotate90(); |
161 |
repaint( true ); |
162 |
} |
163 |
|
164 |
void Board::rotateRight() |
165 |
{ |
166 |
m_sol->rotate90(); |
167 |
m_sol->rotate90(); |
168 |
m_sol->rotate90(); |
169 |
repaint( true ); |
170 |
} |
171 |
|
172 |
void Board::mirrorV() |
173 |
{ |
174 |
m_sol->mirror(); |
175 |
repaint( true ); |
176 |
} |
177 |
|
178 |
void Board::mirrorH() |
179 |
{ |
180 |
m_sol->mirror(); |
181 |
m_sol->rotate90(); |
182 |
m_sol->rotate90(); |
183 |
repaint( true ); |
184 |
} |