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 |
torben |
327 |
#include <q3popupmenu.h> |
23 |
|
|
//Added by qt3to4: |
24 |
|
|
#include <QMouseEvent> |
25 |
|
|
#include <QContextMenuEvent> |
26 |
|
|
#include <QPaintEvent> |
27 |
torben |
1 |
|
28 |
|
|
#include "board.h" |
29 |
|
|
|
30 |
|
|
|
31 |
|
|
Board::Board(QWidget *parent, const char *name) |
32 |
|
|
: QWidget(parent, name) |
33 |
|
|
{ |
34 |
torben |
4 |
m_sol = NULL; |
35 |
torben |
654 |
m_drawmarkers = false; |
36 |
torben |
1 |
} |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
Board::~Board() |
40 |
|
|
{ |
41 |
torben |
4 |
if (m_sol != NULL) |
42 |
|
|
delete m_sol; |
43 |
torben |
1 |
} |
44 |
|
|
|
45 |
torben |
4 |
void Board::setMatrix(Solution* sol) |
46 |
torben |
1 |
{ |
47 |
torben |
6 |
|
48 |
|
|
if (m_sol != NULL) |
49 |
|
|
{ |
50 |
|
|
delete m_sol; |
51 |
|
|
m_sol = NULL; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
if (sol != NULL) |
55 |
torben |
4 |
m_sol = sol->copy(); |
56 |
|
|
|
57 |
torben |
1 |
this->repaint(true); |
58 |
|
|
} |
59 |
|
|
|
60 |
torben |
5 |
|
61 |
torben |
1 |
|
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 |
torben |
4 |
if (m_sol != NULL) { |
71 |
torben |
327 |
QBrush brush( Qt::black); |
72 |
torben |
1 |
p.setBrush( brush ); |
73 |
|
|
for (int i=0; i<m_size; i++) |
74 |
|
|
for (int j=0; j<m_size; j++) { |
75 |
torben |
4 |
if (m_sol->getMatrix(i,j)) |
76 |
torben |
1 |
p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360); |
77 |
|
|
} |
78 |
|
|
} |
79 |
torben |
654 |
|
80 |
|
|
|
81 |
|
|
if (m_drawmarkers == true) { |
82 |
|
|
drawMarkerLines(); |
83 |
|
|
m_drawmarkers = false; |
84 |
|
|
} |
85 |
torben |
1 |
} |
86 |
|
|
|
87 |
torben |
654 |
void Board::drawMarkerLines() |
88 |
torben |
1 |
{ |
89 |
torben |
654 |
QPainter p( this ); |
90 |
|
|
QPen bluePen( Qt::blue ); |
91 |
|
|
p.setPen( bluePen ); |
92 |
|
|
int x = m_markerPoint.x() / 30; |
93 |
|
|
int y = m_markerPoint.y() / 30; |
94 |
torben |
1 |
|
95 |
torben |
654 |
p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) ); |
96 |
|
|
p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) ); |
97 |
torben |
1 |
|
98 |
torben |
654 |
QPen redPen( Qt::red ); |
99 |
|
|
p.setPen( redPen ); |
100 |
torben |
1 |
|
101 |
torben |
654 |
int startx,stopx; |
102 |
|
|
int starty,stopy; |
103 |
|
|
starty = stopy = y; |
104 |
|
|
startx = stopx = x; |
105 |
torben |
1 |
|
106 |
torben |
654 |
// first diagonal line |
107 |
|
|
while (startx > 0 && starty > 0) { |
108 |
|
|
startx--; |
109 |
|
|
starty--; |
110 |
|
|
} |
111 |
torben |
1 |
|
112 |
torben |
654 |
while (stopx < (m_size-1) && stopy < (m_size-1) ) { |
113 |
|
|
stopx++; |
114 |
|
|
stopy++; |
115 |
|
|
} |
116 |
|
|
p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25)); |
117 |
torben |
1 |
|
118 |
torben |
654 |
//next diagonal line |
119 |
|
|
startx = stopx = x; |
120 |
|
|
starty = stopy = y; |
121 |
|
|
while (startx > 0 && starty < (m_size-1) ) { |
122 |
|
|
startx--; |
123 |
|
|
starty++; |
124 |
|
|
} |
125 |
|
|
while (stopx < (m_size-1) && stopy >0) { |
126 |
|
|
stopx++; |
127 |
|
|
stopy--; |
128 |
|
|
} |
129 |
torben |
1 |
|
130 |
torben |
654 |
p.drawLine(QPoint( (startx*30)+5, (starty*30)+25), QPoint( (stopx*30)+25, (stopy*30)+5)); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
void Board::mousePressEvent( QMouseEvent *event) |
134 |
|
|
{ |
135 |
|
|
if (event->button() == Qt::LeftButton) { |
136 |
|
|
m_drawmarkers = true; |
137 |
|
|
m_markerPoint = event->pos(); |
138 |
|
|
this->repaint(true); |
139 |
torben |
1 |
} |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
void Board::setSize( int size ) |
143 |
|
|
{ |
144 |
torben |
4 |
if (m_sol != NULL) |
145 |
|
|
{ |
146 |
|
|
delete m_sol; |
147 |
|
|
m_sol = NULL; |
148 |
|
|
} |
149 |
torben |
6 |
|
150 |
torben |
1 |
m_size = size; |
151 |
|
|
this->setMaximumSize( (size * 30)+1, (size * 30)+1 ); |
152 |
|
|
this->setMinimumSize( size * 30, size * 30); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
void Board::contextMenuEvent( QContextMenuEvent *event) |
156 |
|
|
{ |
157 |
torben |
4 |
if (m_sol == NULL) |
158 |
|
|
return; |
159 |
|
|
|
160 |
torben |
327 |
Q3PopupMenu *contextMenu = new Q3PopupMenu( this ); |
161 |
torben |
1 |
contextMenu->setCheckable( false ); |
162 |
|
|
contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) ); |
163 |
|
|
contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) ); |
164 |
|
|
contextMenu->insertItem("Vertical mirror", this, SLOT ( mirrorV() ) ); |
165 |
|
|
contextMenu->insertItem("Horisontal mirror", this, SLOT( mirrorH() ) ); |
166 |
|
|
|
167 |
|
|
contextMenu->exec( event->globalPos() ); |
168 |
|
|
delete contextMenu; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
void Board::rotateLeft() |
172 |
|
|
{ |
173 |
torben |
4 |
m_sol->rotate90(); |
174 |
torben |
1 |
repaint( true ); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
void Board::rotateRight() |
178 |
|
|
{ |
179 |
torben |
4 |
m_sol->rotate90(); |
180 |
|
|
m_sol->rotate90(); |
181 |
|
|
m_sol->rotate90(); |
182 |
torben |
1 |
repaint( true ); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
void Board::mirrorV() |
186 |
|
|
{ |
187 |
torben |
4 |
m_sol->mirror(); |
188 |
torben |
1 |
repaint( true ); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
void Board::mirrorH() |
192 |
|
|
{ |
193 |
torben |
4 |
m_sol->mirror(); |
194 |
|
|
m_sol->rotate90(); |
195 |
|
|
m_sol->rotate90(); |
196 |
torben |
1 |
repaint( true ); |
197 |
|
|
} |