20 |
#include <qapplication.h> |
#include <qapplication.h> |
21 |
#include <qlabel.h> |
#include <qlabel.h> |
22 |
#include <qlayout.h> |
#include <qlayout.h> |
23 |
#include <qlistbox.h> |
#include <q3listbox.h> |
24 |
#include <qpopupmenu.h> |
#include <q3popupmenu.h> |
25 |
#include <qpushbutton.h> |
#include <qpushbutton.h> |
26 |
#include <qspinbox.h> |
#include <qspinbox.h> |
27 |
#include <qstring.h> |
#include <qstring.h> |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
#include "queensmain.h" |
#include "queensmain.h" |
32 |
|
//Added by qt3to4: |
33 |
|
#include <Q3HBoxLayout> |
34 |
|
#include <QContextMenuEvent> |
35 |
|
#include <Q3VBoxLayout> |
36 |
#include "board.h" |
#include "board.h" |
37 |
#include "queens.h" |
#include "queens.h" |
38 |
#include "solutionvector.h" |
|
39 |
#include "solutionlist.h" |
#include "solution.h" |
40 |
#include "solutionhash.h" |
#include "solutionmatrix.h" |
41 |
#include "solutionmnvector.h" |
#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" |
#include "config.h" |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
QueensMain::QueensMain(QWidget *parent, const char *name) |
QueensMain::QueensMain(QWidget *parent, const char *name) |
54 |
: QDialog(parent, name) |
: QDialog(parent, name) |
55 |
{ |
{ |
56 |
QVBoxLayout *mainlayout = new QVBoxLayout( this ); |
Q3VBoxLayout *mainlayout = new Q3VBoxLayout( this ); |
57 |
|
|
58 |
m_board = new Board( this ); |
m_board = new Board( this ); |
59 |
|
|
62 |
m_stop = new QPushButton("Stop", this); |
m_stop = new QPushButton("Stop", this); |
63 |
m_stop->setEnabled( false ); |
m_stop->setEnabled( false ); |
64 |
|
|
65 |
QHBoxLayout *upperlayout = new QHBoxLayout( mainlayout ); |
Q3HBoxLayout *upperlayout = new Q3HBoxLayout( mainlayout ); |
66 |
QVBoxLayout *left = new QVBoxLayout( upperlayout ); |
Q3VBoxLayout *left = new Q3VBoxLayout( upperlayout ); |
67 |
|
|
68 |
upperlayout->add( m_board ); |
upperlayout->add( m_board ); |
69 |
m_list = new QListBox( this ); |
m_list = new Q3ListBox( this ); |
70 |
|
|
71 |
m_list->setMinimumWidth( 180 ); |
m_list->setMinimumWidth( 180 ); |
72 |
m_sizeSelector = new QSpinBox( this ); |
m_sizeSelector = new QSpinBox( this ); |
75 |
left->add( m_list ); |
left->add( m_list ); |
76 |
left->add( m_sizeSelector ); |
left->add( m_sizeSelector ); |
77 |
|
|
78 |
QHBoxLayout *buttons = new QHBoxLayout( mainlayout ); |
Q3HBoxLayout *buttons = new Q3HBoxLayout( mainlayout ); |
79 |
buttons->add( m_start ); |
buttons->add( m_start ); |
80 |
buttons->add( m_stop ); |
buttons->add( m_stop ); |
81 |
buttons->add( m_quit ); |
buttons->add( m_quit ); |
87 |
m_sizeSelector->setValue( 8 ); |
m_sizeSelector->setValue( 8 ); |
88 |
m_board->setSize( 8 ); |
m_board->setSize( 8 ); |
89 |
|
|
90 |
m_sortalgo = SortList; |
m_storage = StorageInt; |
91 |
|
m_sortalgo = SortMinimalVector; |
92 |
m_solutions = NULL; |
m_solutions = NULL; |
93 |
m_queens = NULL; |
m_queens = NULL; |
94 |
|
m_sol = NULL; |
95 |
|
|
96 |
connect(m_quit, |
connect(m_quit, |
97 |
SIGNAL( clicked() ), |
SIGNAL( clicked() ), |
134 |
QueensMain::~QueensMain() |
QueensMain::~QueensMain() |
135 |
{ |
{ |
136 |
delete m_board; |
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() |
void QueensMain::start() |
149 |
m_list->clear(); |
m_list->clear(); |
150 |
m_status->setText( QString("Searching ...") ); |
m_status->setText( QString("Searching ...") ); |
151 |
|
|
152 |
|
m_board->setMatrix(0); |
153 |
|
|
154 |
if (m_queens != NULL) { |
if (m_queens != NULL) { |
155 |
m_queens->wait(); |
m_queens->wait(); |
156 |
delete m_queens; |
delete m_queens; |
157 |
|
m_queens = 0; |
158 |
} |
} |
159 |
|
|
160 |
if (m_solutions != NULL) |
if (m_solutions != NULL) |
161 |
delete m_solutions; |
delete m_solutions; |
162 |
switch (m_sortalgo) { |
switch (m_sortalgo) { |
163 |
case SortList: |
case SortList: |
164 |
m_solutions = new SolutionList(this); |
m_solutions = new ContainerList(this); |
165 |
break; |
break; |
166 |
case SortVector: |
case SortVector: |
167 |
m_solutions = new SolutionVector(this); |
m_solutions = new ContainerVector(this); |
168 |
break; |
break; |
169 |
case SortHash: |
case SortHash: |
170 |
m_solutions = new SolutionHash(this); |
m_solutions = new ContainerHash(this); |
171 |
break; |
break; |
172 |
case SortMNVector: |
case SortMNVector: |
173 |
m_solutions = new SolutionMNVector(this); |
m_solutions = new ContainerMNVector(this); |
174 |
break; |
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 |
|
|
193 |
m_elapsed.start(); |
m_elapsed.start(); |
194 |
m_time.start(); |
m_time.start(); |
195 |
m_queens = new Queens(this, m_solutions, m_sizeSelector->value() ,false); |
m_queens = new Queens(this, m_sol, m_solutions, m_sizeSelector->value() ,false); |
196 |
m_queens->start(); |
m_queens->start(); |
197 |
} |
} |
198 |
|
|
219 |
|
|
220 |
void QueensMain::foundSolution() |
void QueensMain::foundSolution() |
221 |
{ |
{ |
222 |
int num = m_solutions->numSolutions(); |
int num = m_solutions->totalSolutions(); |
223 |
if (m_elapsed.elapsed() > 500) |
if (m_elapsed.elapsed() > 500) |
224 |
{ |
{ |
225 |
m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") ); |
m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") ); |
239 |
void QueensMain::showSolution() |
void QueensMain::showSolution() |
240 |
{ |
{ |
241 |
int index = m_list->currentItem(); |
int index = m_list->currentItem(); |
242 |
Solution sol = m_solutions->solution( index ); |
Solution* sol = m_solutions->solution( index ); |
243 |
m_board->setMatrix( sol ); |
m_board->setMatrix( sol ); |
244 |
} |
} |
245 |
|
|
270 |
|
|
271 |
void QueensMain::uniqueSolutions() |
void QueensMain::uniqueSolutions() |
272 |
{ |
{ |
273 |
m_totalcount = m_solutions->numSolutions(); |
m_totalcount = m_solutions->totalSolutions(); |
274 |
m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") ); |
m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") ); |
275 |
|
|
276 |
m_elapsed.restart(); |
m_elapsed.restart(); |
291 |
|
|
292 |
void QueensMain::contextMenuEvent( QContextMenuEvent *event) |
void QueensMain::contextMenuEvent( QContextMenuEvent *event) |
293 |
{ |
{ |
294 |
QPopupMenu *contextMenu = new QPopupMenu( this ); |
Q3PopupMenu *contextMenu = new Q3PopupMenu( this ); |
295 |
contextMenu->setCheckable( true ); |
contextMenu->setCheckable( true ); |
296 |
QLabel *caption = new QLabel("<b><i>Sort algorithm</i></b>", this); |
contextMenu->insertItem( "---Container class---" ); |
|
caption->setAlignment( Qt::AlignCenter ); |
|
|
contextMenu->insertItem( caption ); |
|
297 |
|
|
298 |
contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1); |
contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1); |
299 |
contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2); |
contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2); |
300 |
contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3); |
contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3); |
301 |
contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4); |
contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4); |
302 |
|
contextMenu->insertItem("Minimal Vector", this, SLOT(sortMinimalVector()), 0, 5); |
303 |
|
|
304 |
|
contextMenu->insertItem( "---Solution Class---" ); |
305 |
|
contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 6); |
306 |
|
contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 7); |
307 |
|
|
308 |
|
|
309 |
switch (m_sortalgo) { |
switch (m_sortalgo) { |
310 |
case SortList: |
case SortList: |
319 |
case SortMNVector: |
case SortMNVector: |
320 |
contextMenu->setItemChecked( 4, true); |
contextMenu->setItemChecked( 4, true); |
321 |
break; |
break; |
322 |
|
case SortMinimalVector: |
323 |
|
contextMenu->setItemChecked( 5, true); |
324 |
|
break; |
325 |
} |
} |
326 |
|
|
327 |
|
switch (m_storage) { |
328 |
|
case StorageMatrix: |
329 |
|
contextMenu->setItemChecked(6, true); |
330 |
|
break; |
331 |
|
case StorageInt: |
332 |
|
contextMenu->setItemChecked(7, true); |
333 |
|
break; |
334 |
|
} |
335 |
|
|
336 |
|
|
337 |
contextMenu->exec( event->globalPos() ); |
contextMenu->exec( event->globalPos() ); |
338 |
delete contextMenu; |
delete contextMenu; |
339 |
} |
} |
370 |
m_sortalgo = SortMNVector; |
m_sortalgo = SortMNVector; |
371 |
} |
} |
372 |
|
|
373 |
|
void QueensMain::sortMinimalVector() |
374 |
|
{ |
375 |
|
m_sortalgo = SortMinimalVector; |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
void QueensMain::storageInt() |
380 |
|
{ |
381 |
|
m_storage = StorageInt; |
382 |
|
} |
383 |
|
|
384 |
|
void QueensMain::storageMatrix() |
385 |
|
{ |
386 |
|
m_storage = StorageMatrix; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
|
|