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> |
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 |
|
|
39 |
#include "solution.h" |
#include "solution.h" |
40 |
#include "solutionvector.h" |
#include "solutionmatrix.h" |
41 |
#include "solutionlist.h" |
#include "solutionint.h" |
42 |
#include "solutionhash.h" |
|
43 |
#include "solutionmnvector.h" |
#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 |
#define MatrixSolution Solution |
|
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_storage = StorageMatrix; |
m_storage = StorageInt; |
91 |
m_sortalgo = SortList; |
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) |
if (m_sol != NULL) |
181 |
delete m_sol; |
delete m_sol; |
182 |
|
/* |
183 |
switch(this->m_storage) { |
switch(this->m_storage) { |
184 |
case StorageInt: |
case StorageInt: |
185 |
#warning MatrixSolution<=>IntSolution |
m_sol = new SolutionInt(m_sizeSelector->value()); |
|
m_sol = new MatrixSolution(m_sizeSelector->value()); |
|
186 |
break; |
break; |
187 |
case StorageMatrix: |
case StorageMatrix: |
188 |
m_sol = new MatrixSolution(m_sizeSelector->value()); |
m_sol = new SolutionMatrix(m_sizeSelector->value()); |
189 |
break; |
break; |
190 |
} |
}*/ |
191 |
|
|
192 |
|
m_sol = new Solution( m_sizeSelector->value() ); |
193 |
|
|
194 |
|
|
195 |
m_elapsed.start(); |
m_elapsed.start(); |
196 |
m_time.start(); |
m_time.start(); |
197 |
m_queens = new Queens(this, m_solutions, m_sizeSelector->value() ,false); |
m_queens = new Queens(this, m_sol, m_solutions, m_sizeSelector->value() ,false); |
198 |
m_queens->start(); |
m_queens->start(); |
199 |
} |
} |
200 |
|
|
221 |
|
|
222 |
void QueensMain::foundSolution() |
void QueensMain::foundSolution() |
223 |
{ |
{ |
224 |
int num = m_solutions->numSolutions(); |
int num = m_solutions->totalSolutions(); |
225 |
if (m_elapsed.elapsed() > 500) |
if (m_elapsed.elapsed() > 500) |
226 |
{ |
{ |
227 |
m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") ); |
m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") ); |
242 |
{ |
{ |
243 |
int index = m_list->currentItem(); |
int index = m_list->currentItem(); |
244 |
Solution* sol = m_solutions->solution( index ); |
Solution* sol = m_solutions->solution( index ); |
245 |
m_board->setMatrix( *sol ); |
m_board->setMatrix( sol ); |
246 |
} |
} |
247 |
|
|
248 |
void QueensMain::resize(int size) |
void QueensMain::resize(int size) |
272 |
|
|
273 |
void QueensMain::uniqueSolutions() |
void QueensMain::uniqueSolutions() |
274 |
{ |
{ |
275 |
m_totalcount = m_solutions->numSolutions(); |
m_totalcount = m_solutions->totalSolutions(); |
276 |
m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") ); |
m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") ); |
277 |
|
|
278 |
m_elapsed.restart(); |
m_elapsed.restart(); |
293 |
|
|
294 |
void QueensMain::contextMenuEvent( QContextMenuEvent *event) |
void QueensMain::contextMenuEvent( QContextMenuEvent *event) |
295 |
{ |
{ |
296 |
QPopupMenu *contextMenu = new QPopupMenu( this ); |
Q3PopupMenu *contextMenu = new Q3PopupMenu( this ); |
297 |
contextMenu->setCheckable( true ); |
contextMenu->setCheckable( true ); |
298 |
QLabel *sortCaption = new QLabel("<b><i>Sort algorithm</i></b>", this); |
contextMenu->insertItem( "---Container class---" ); |
|
sortCaption->setAlignment( Qt::AlignCenter ); |
|
|
contextMenu->insertItem( sortCaption ); |
|
299 |
|
|
300 |
contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1); |
contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1); |
301 |
contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2); |
contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2); |
302 |
contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3); |
contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3); |
303 |
contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4); |
contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4); |
304 |
|
contextMenu->insertItem("Minimal Vector", this, SLOT(sortMinimalVector()), 0, 5); |
305 |
|
|
306 |
QLabel *storageCaption = new QLabel("<b><i>Storage class</i></b>", this); |
contextMenu->insertItem( "---Solution Class---" ); |
307 |
contextMenu->insertItem( storageCaption ); |
contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 6); |
308 |
contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 5); |
contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 7); |
|
contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 6); |
|
309 |
|
|
310 |
|
|
311 |
switch (m_sortalgo) { |
switch (m_sortalgo) { |
321 |
case SortMNVector: |
case SortMNVector: |
322 |
contextMenu->setItemChecked( 4, true); |
contextMenu->setItemChecked( 4, true); |
323 |
break; |
break; |
324 |
|
case SortMinimalVector: |
325 |
|
contextMenu->setItemChecked( 5, true); |
326 |
|
break; |
327 |
} |
} |
328 |
|
|
329 |
switch (m_storage) { |
switch (m_storage) { |
330 |
case StorageMatrix: |
case StorageMatrix: |
331 |
contextMenu->setItemChecked(5, true); |
contextMenu->setItemChecked(6, true); |
332 |
break; |
break; |
333 |
case StorageInt: |
case StorageInt: |
334 |
contextMenu->setItemChecked(6, true); |
contextMenu->setItemChecked(7, true); |
335 |
break; |
break; |
336 |
} |
} |
337 |
|
|
372 |
m_sortalgo = SortMNVector; |
m_sortalgo = SortMNVector; |
373 |
} |
} |
374 |
|
|
375 |
|
void QueensMain::sortMinimalVector() |
376 |
|
{ |
377 |
|
m_sortalgo = SortMinimalVector; |
378 |
|
} |
379 |
|
|
380 |
|
|
381 |
void QueensMain::storageInt() |
void QueensMain::storageInt() |
382 |
{ |
{ |
383 |
m_storage = StorageInt; |
m_storage = StorageInt; |
389 |
} |
} |
390 |
|
|
391 |
|
|
392 |
|
|