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 <qapplication.h> |
21 |
#include <qlabel.h> |
22 |
#include <qlayout.h> |
23 |
#include <q3listbox.h> |
24 |
#include <q3popupmenu.h> |
25 |
#include <qpushbutton.h> |
26 |
#include <qspinbox.h> |
27 |
#include <qstring.h> |
28 |
|
29 |
|
30 |
|
31 |
#include "queensmain.h" |
32 |
//Added by qt3to4: |
33 |
#include <Q3HBoxLayout> |
34 |
#include <QContextMenuEvent> |
35 |
#include <Q3VBoxLayout> |
36 |
#include "board.h" |
37 |
#include "queens.h" |
38 |
|
39 |
#include "solution.h" |
40 |
#include "solutionmatrix.h" |
41 |
#include "solutionint.h" |
42 |
|
43 |
#include "containervector.h" |
44 |
#include "containerlist.h" |
45 |
#include "containerhash.h" |
46 |
#include "containermnvector.h" |
47 |
#include "containerminimalvector.h" |
48 |
|
49 |
#include "config.h" |
50 |
|
51 |
|
52 |
|
53 |
QueensMain::QueensMain(QWidget *parent, const char *name) |
54 |
: QDialog(parent, name) |
55 |
{ |
56 |
Q3VBoxLayout *mainlayout = new Q3VBoxLayout( this ); |
57 |
|
58 |
m_board = new Board( this ); |
59 |
|
60 |
m_start = new QPushButton("Start", this); |
61 |
m_quit = new QPushButton("Quit", this); |
62 |
m_stop = new QPushButton("Stop", this); |
63 |
m_stop->setEnabled( false ); |
64 |
|
65 |
Q3HBoxLayout *upperlayout = new Q3HBoxLayout( mainlayout ); |
66 |
Q3VBoxLayout *left = new Q3VBoxLayout( upperlayout ); |
67 |
|
68 |
upperlayout->add( m_board ); |
69 |
m_list = new Q3ListBox( this ); |
70 |
|
71 |
m_list->setMinimumWidth( 180 ); |
72 |
m_sizeSelector = new QSpinBox( this ); |
73 |
m_status = new QLabel( this ); |
74 |
|
75 |
left->add( m_list ); |
76 |
left->add( m_sizeSelector ); |
77 |
|
78 |
Q3HBoxLayout *buttons = new Q3HBoxLayout( mainlayout ); |
79 |
buttons->add( m_start ); |
80 |
buttons->add( m_stop ); |
81 |
buttons->add( m_quit ); |
82 |
|
83 |
mainlayout->add( m_status ); |
84 |
|
85 |
m_sizeSelector->setMinValue( MIN_SIZE ); |
86 |
m_sizeSelector->setMaxValue( MAX_SIZE ); |
87 |
m_sizeSelector->setValue( 8 ); |
88 |
m_board->setSize( 8 ); |
89 |
|
90 |
m_storage = StorageInt; |
91 |
m_sortalgo = SortMinimalVector; |
92 |
m_solutions = NULL; |
93 |
m_queens = NULL; |
94 |
m_sol = NULL; |
95 |
|
96 |
connect(m_quit, |
97 |
SIGNAL( clicked() ), |
98 |
qApp, |
99 |
SLOT( quit() ) |
100 |
); |
101 |
|
102 |
connect(m_sizeSelector, |
103 |
SIGNAL( valueChanged(int) ), |
104 |
this, |
105 |
SLOT( resize(int) ) |
106 |
); |
107 |
|
108 |
connect(m_start, |
109 |
SIGNAL( clicked() ), |
110 |
this, |
111 |
SLOT( start() ) |
112 |
); |
113 |
|
114 |
connect(m_stop, |
115 |
SIGNAL( clicked() ), |
116 |
this, |
117 |
SLOT( stop() ) |
118 |
); |
119 |
|
120 |
connect(m_list, |
121 |
SIGNAL( selectionChanged() ), |
122 |
this, |
123 |
SLOT( showSolution() ) |
124 |
); |
125 |
|
126 |
connect( qApp, |
127 |
SIGNAL( lastWindowClosed() ), |
128 |
qApp, |
129 |
SLOT( quit() ) |
130 |
); |
131 |
} |
132 |
|
133 |
|
134 |
QueensMain::~QueensMain() |
135 |
{ |
136 |
delete m_board; |
137 |
|
138 |
if (m_solutions != NULL) |
139 |
delete m_solutions; |
140 |
if (m_sol != NULL) |
141 |
delete m_sol; |
142 |
} |
143 |
|
144 |
void QueensMain::start() |
145 |
{ |
146 |
m_sizeSelector->setEnabled( false ); |
147 |
m_start->setEnabled( false ); |
148 |
m_stop->setEnabled( true ); |
149 |
m_list->clear(); |
150 |
m_status->setText( QString("Searching ...") ); |
151 |
|
152 |
m_board->setMatrix(0); |
153 |
|
154 |
if (m_queens != NULL) { |
155 |
m_queens->wait(); |
156 |
delete m_queens; |
157 |
m_queens = 0; |
158 |
} |
159 |
|
160 |
if (m_solutions != NULL) |
161 |
delete m_solutions; |
162 |
switch (m_sortalgo) { |
163 |
case SortList: |
164 |
m_solutions = new ContainerList(this); |
165 |
break; |
166 |
case SortVector: |
167 |
m_solutions = new ContainerVector(this); |
168 |
break; |
169 |
case SortHash: |
170 |
m_solutions = new ContainerHash(this); |
171 |
break; |
172 |
case SortMNVector: |
173 |
m_solutions = new ContainerMNVector(this); |
174 |
break; |
175 |
case SortMinimalVector: |
176 |
m_solutions = new ContainerMinimalVector(this); |
177 |
break; |
178 |
} |
179 |
|
180 |
if (m_sol != NULL) |
181 |
delete m_sol; |
182 |
/* |
183 |
switch(this->m_storage) { |
184 |
case StorageInt: |
185 |
m_sol = new SolutionInt(m_sizeSelector->value()); |
186 |
break; |
187 |
case StorageMatrix: |
188 |
m_sol = new SolutionMatrix(m_sizeSelector->value()); |
189 |
break; |
190 |
}*/ |
191 |
|
192 |
m_sol = new Solution( m_sizeSelector->value() ); |
193 |
|
194 |
|
195 |
m_elapsed.start(); |
196 |
m_time.start(); |
197 |
m_queens = new Queens(this, m_sol, m_solutions, m_sizeSelector->value() ,false); |
198 |
m_queens->start(); |
199 |
} |
200 |
|
201 |
void QueensMain::stop() |
202 |
{ |
203 |
m_solutions->halt(); |
204 |
m_queens->stop(); |
205 |
m_queens->wait(); |
206 |
|
207 |
int num = m_solutions->numSolutions(); |
208 |
m_status->setText( QString("Aborted. Found ") + QString::number(num,10).append(" solutions") ); |
209 |
m_list->clear(); |
210 |
for (int i=1; i<=num; i++) |
211 |
m_list->insertItem( QString("Solution no ") + QString::number(i,10), i); |
212 |
|
213 |
delete m_queens; |
214 |
m_queens = NULL; |
215 |
|
216 |
m_list->setEnabled( true ); |
217 |
m_sizeSelector->setEnabled( true ); |
218 |
m_start->setEnabled( true ); |
219 |
m_stop->setEnabled( false ); |
220 |
} |
221 |
|
222 |
void QueensMain::foundSolution() |
223 |
{ |
224 |
int num = m_solutions->totalSolutions(); |
225 |
if (m_elapsed.elapsed() > 500) |
226 |
{ |
227 |
m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") ); |
228 |
m_elapsed.restart(); |
229 |
} |
230 |
} |
231 |
|
232 |
void QueensMain::finishedSearch() |
233 |
{ |
234 |
uniqueSolutions(); |
235 |
m_sizeSelector->setEnabled( true ); |
236 |
m_start->setEnabled( true ); |
237 |
m_stop->setEnabled( false ); |
238 |
} |
239 |
|
240 |
|
241 |
void QueensMain::showSolution() |
242 |
{ |
243 |
int index = m_list->currentItem(); |
244 |
Solution* sol = m_solutions->solution( index ); |
245 |
m_board->setMatrix( sol ); |
246 |
} |
247 |
|
248 |
void QueensMain::resize(int size) |
249 |
{ |
250 |
m_board->setSize( size ); |
251 |
m_board->repaint( true ); |
252 |
m_list->clear(); |
253 |
} |
254 |
|
255 |
QString QueensMain::elapsed() |
256 |
{ |
257 |
int time = m_time.elapsed(); |
258 |
int msec = time % 1000; |
259 |
int sec = time / 1000; |
260 |
int min = sec / 60; |
261 |
sec %= 60; |
262 |
QString smsec = (QString::number(msec)).rightJustify(3, '0'); |
263 |
QString ssec = (QString::number(sec)).rightJustify(2,'0'); |
264 |
|
265 |
QString res; |
266 |
if (min!=0) |
267 |
res = QString("%1:%2.%3").arg(min).arg(ssec).arg(smsec); |
268 |
else |
269 |
res = QString("%1.%2").arg(sec).arg(smsec); |
270 |
return res; |
271 |
} |
272 |
|
273 |
void QueensMain::uniqueSolutions() |
274 |
{ |
275 |
m_totalcount = m_solutions->totalSolutions(); |
276 |
m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") ); |
277 |
|
278 |
m_elapsed.restart(); |
279 |
m_solutions->uniqueSolutions(); |
280 |
|
281 |
int uniq = m_solutions->numSolutions(); |
282 |
|
283 |
|
284 |
QString msg; |
285 |
msg = QString( "Found %1 unique solutions, of %2 total solutions. Time elapsed: %3" ).arg(uniq).arg(m_totalcount).arg( elapsed() ); |
286 |
|
287 |
m_list->clear(); |
288 |
for (int i=1; i<=uniq; i++) |
289 |
m_list->insertItem( QString("Unique solution no ") + QString::number(i,10), i); |
290 |
|
291 |
m_status->setText( msg ); |
292 |
} |
293 |
|
294 |
void QueensMain::contextMenuEvent( QContextMenuEvent *event) |
295 |
{ |
296 |
Q3PopupMenu *contextMenu = new Q3PopupMenu( this ); |
297 |
contextMenu->setCheckable( true ); |
298 |
contextMenu->insertItem( "---Container class---" ); |
299 |
|
300 |
contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1); |
301 |
contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2); |
302 |
contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3); |
303 |
contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4); |
304 |
contextMenu->insertItem("Minimal Vector", this, SLOT(sortMinimalVector()), 0, 5); |
305 |
|
306 |
contextMenu->insertItem( "---Solution Class---" ); |
307 |
contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 6); |
308 |
contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 7); |
309 |
|
310 |
|
311 |
switch (m_sortalgo) { |
312 |
case SortList: |
313 |
contextMenu->setItemChecked( 1, true); |
314 |
break; |
315 |
case SortVector: |
316 |
contextMenu->setItemChecked( 2, true); |
317 |
break; |
318 |
case SortHash: |
319 |
contextMenu->setItemChecked( 3, true); |
320 |
break; |
321 |
case SortMNVector: |
322 |
contextMenu->setItemChecked( 4, true); |
323 |
break; |
324 |
case SortMinimalVector: |
325 |
contextMenu->setItemChecked( 5, true); |
326 |
break; |
327 |
} |
328 |
|
329 |
switch (m_storage) { |
330 |
case StorageMatrix: |
331 |
contextMenu->setItemChecked(6, true); |
332 |
break; |
333 |
case StorageInt: |
334 |
contextMenu->setItemChecked(7, true); |
335 |
break; |
336 |
} |
337 |
|
338 |
|
339 |
contextMenu->exec( event->globalPos() ); |
340 |
delete contextMenu; |
341 |
} |
342 |
|
343 |
void QueensMain::duplicateRemoved() |
344 |
{ |
345 |
if (m_elapsed.elapsed() > 500) |
346 |
{ |
347 |
int uniq = m_solutions->getUniqueRemoved(); |
348 |
QString status; |
349 |
status = QString ("Sorting %1 solutions. So far identified %2 duplicates").arg(m_totalcount).arg(uniq); |
350 |
m_status->setText(status); |
351 |
m_elapsed.restart(); |
352 |
} |
353 |
} |
354 |
|
355 |
void QueensMain::sortList() |
356 |
{ |
357 |
m_sortalgo = SortList; |
358 |
} |
359 |
|
360 |
void QueensMain::sortVector() |
361 |
{ |
362 |
m_sortalgo = SortVector; |
363 |
} |
364 |
|
365 |
void QueensMain::sortHash() |
366 |
{ |
367 |
m_sortalgo = SortHash; |
368 |
} |
369 |
|
370 |
void QueensMain::sortMNVector() |
371 |
{ |
372 |
m_sortalgo = SortMNVector; |
373 |
} |
374 |
|
375 |
void QueensMain::sortMinimalVector() |
376 |
{ |
377 |
m_sortalgo = SortMinimalVector; |
378 |
} |
379 |
|
380 |
|
381 |
void QueensMain::storageInt() |
382 |
{ |
383 |
m_storage = StorageInt; |
384 |
} |
385 |
|
386 |
void QueensMain::storageMatrix() |
387 |
{ |
388 |
m_storage = StorageMatrix; |
389 |
} |