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