/[projects]/queensgui/src/queensmain.cpp
ViewVC logotype

Contents of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations) (download)
Thu Jul 19 23:44:01 2007 UTC (16 years, 10 months ago) by torben
File size: 9129 byte(s)
Renamed solution{list,vector,hash,nmvector} to container{...}


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

Properties

Name Value
svn:eol-style native
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20