19 |
***************************************************************************/ |
***************************************************************************/ |
20 |
#include <qstring.h> |
#include <qstring.h> |
21 |
#include <qpainter.h> |
#include <qpainter.h> |
22 |
#include <qpopupmenu.h> |
#include <q3popupmenu.h> |
23 |
|
//Added by qt3to4: |
24 |
|
#include <QMouseEvent> |
25 |
|
#include <QContextMenuEvent> |
26 |
|
#include <QPaintEvent> |
27 |
|
|
28 |
#include "board.h" |
#include "board.h" |
29 |
|
|
31 |
Board::Board(QWidget *parent, const char *name) |
Board::Board(QWidget *parent, const char *name) |
32 |
: QWidget(parent, name) |
: QWidget(parent, name) |
33 |
{ |
{ |
34 |
m_hasData = false; |
m_sol = NULL; |
35 |
} |
} |
36 |
|
|
37 |
|
|
38 |
Board::~Board() |
Board::~Board() |
39 |
{ |
{ |
40 |
|
if (m_sol != NULL) |
41 |
|
delete m_sol; |
42 |
} |
} |
43 |
|
|
44 |
void Board::setMatrix(Solution sol) |
void Board::setMatrix(Solution* sol) |
45 |
{ |
{ |
|
m_hasData = true; |
|
|
m_sol = sol; |
|
|
this->repaint(true); |
|
|
} |
|
46 |
|
|
47 |
void Board::setMatrix(IntSolution sol) |
if (m_sol != NULL) |
48 |
{ |
{ |
49 |
m_hasData = true; |
delete m_sol; |
50 |
m_sol = Solution(); |
m_sol = NULL; |
51 |
for (int i=0;i<m_size;i++) |
} |
52 |
m_sol.matrix[i][ sol.imatrix[i] ] = true; |
|
53 |
|
if (sol != NULL) |
54 |
|
m_sol = sol->copy(); |
55 |
|
|
56 |
this->repaint(true); |
this->repaint(true); |
57 |
} |
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
void Board::paintEvent( QPaintEvent *event ) |
void Board::paintEvent( QPaintEvent *event ) |
62 |
{ |
{ |
63 |
QPainter p (this); |
QPainter p (this); |
66 |
p.drawLine(i*30, 0, i*30, m_size*30); |
p.drawLine(i*30, 0, i*30, m_size*30); |
67 |
} |
} |
68 |
|
|
69 |
if (m_hasData) { |
if (m_sol != NULL) { |
70 |
QBrush brush( black); |
QBrush brush( Qt::black); |
71 |
p.setBrush( brush ); |
p.setBrush( brush ); |
72 |
for (int i=0; i<m_size; i++) |
for (int i=0; i<m_size; i++) |
73 |
for (int j=0; j<m_size; j++) { |
for (int j=0; j<m_size; j++) { |
74 |
if (m_sol.matrix[i][j]) |
if (m_sol->getMatrix(i,j)) |
75 |
p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360); |
p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360); |
76 |
} |
} |
77 |
} |
} |
79 |
|
|
80 |
void Board::mousePressEvent( QMouseEvent *event) |
void Board::mousePressEvent( QMouseEvent *event) |
81 |
{ |
{ |
82 |
if (event->button() == LeftButton) { |
if (event->button() == Qt::LeftButton) { |
83 |
this->repaint(true); |
this->repaint(true); |
84 |
QPainter p( this ); |
QPainter p( this ); |
85 |
QPen bluePen( blue ); |
QPen bluePen( Qt::blue ); |
86 |
p.setPen( bluePen ); |
p.setPen( bluePen ); |
87 |
int x = event->x() / 30; |
int x = event->x() / 30; |
88 |
int y = event->y() / 30; |
int y = event->y() / 30; |
90 |
p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) ); |
p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) ); |
91 |
p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) ); |
p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) ); |
92 |
|
|
93 |
QPen redPen( red ); |
QPen redPen( Qt::red ); |
94 |
p.setPen( redPen ); |
p.setPen( redPen ); |
95 |
|
|
96 |
int startx,stopx; |
int startx,stopx; |
98 |
starty = stopy = y; |
starty = stopy = y; |
99 |
startx = stopx = x; |
startx = stopx = x; |
100 |
|
|
101 |
// første skrå linie |
// f�rste skr� linie |
102 |
while (startx > 0 && starty > 0) { |
while (startx > 0 && starty > 0) { |
103 |
startx--; |
startx--; |
104 |
starty--; |
starty--; |
110 |
} |
} |
111 |
p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25)); |
p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25)); |
112 |
|
|
113 |
//næste skrå linie |
//n�ste skr� linie |
114 |
startx = stopx = x; |
startx = stopx = x; |
115 |
starty = stopy = y; |
starty = stopy = y; |
116 |
while (startx > 0 && starty < (m_size-1) ) { |
while (startx > 0 && starty < (m_size-1) ) { |
128 |
|
|
129 |
void Board::setSize( int size ) |
void Board::setSize( int size ) |
130 |
{ |
{ |
131 |
m_hasData = false; |
if (m_sol != NULL) |
132 |
|
{ |
133 |
|
delete m_sol; |
134 |
|
m_sol = NULL; |
135 |
|
} |
136 |
|
|
137 |
m_size = size; |
m_size = size; |
138 |
this->setMaximumSize( (size * 30)+1, (size * 30)+1 ); |
this->setMaximumSize( (size * 30)+1, (size * 30)+1 ); |
139 |
this->setMinimumSize( size * 30, size * 30); |
this->setMinimumSize( size * 30, size * 30); |
141 |
|
|
142 |
void Board::contextMenuEvent( QContextMenuEvent *event) |
void Board::contextMenuEvent( QContextMenuEvent *event) |
143 |
{ |
{ |
144 |
QPopupMenu *contextMenu = new QPopupMenu( this ); |
if (m_sol == NULL) |
145 |
|
return; |
146 |
|
|
147 |
|
Q3PopupMenu *contextMenu = new Q3PopupMenu( this ); |
148 |
contextMenu->setCheckable( false ); |
contextMenu->setCheckable( false ); |
149 |
contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) ); |
contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) ); |
150 |
contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) ); |
contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) ); |
157 |
|
|
158 |
void Board::rotateLeft() |
void Board::rotateLeft() |
159 |
{ |
{ |
160 |
m_sol.rotate90(); |
m_sol->rotate90(); |
161 |
repaint( true ); |
repaint( true ); |
162 |
} |
} |
163 |
|
|
164 |
void Board::rotateRight() |
void Board::rotateRight() |
165 |
{ |
{ |
166 |
m_sol.rotate90(); |
m_sol->rotate90(); |
167 |
m_sol.rotate90(); |
m_sol->rotate90(); |
168 |
m_sol.rotate90(); |
m_sol->rotate90(); |
169 |
repaint( true ); |
repaint( true ); |
170 |
} |
} |
171 |
|
|
172 |
void Board::mirrorV() |
void Board::mirrorV() |
173 |
{ |
{ |
174 |
m_sol.mirror(); |
m_sol->mirror(); |
175 |
repaint( true ); |
repaint( true ); |
176 |
} |
} |
177 |
|
|
178 |
void Board::mirrorH() |
void Board::mirrorH() |
179 |
{ |
{ |
180 |
m_sol.mirror(); |
m_sol->mirror(); |
181 |
m_sol.rotate90(); |
m_sol->rotate90(); |
182 |
m_sol.rotate90(); |
m_sol->rotate90(); |
183 |
repaint( true ); |
repaint( true ); |
184 |
} |
} |