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