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

Contents of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (show annotations) (download)
Fri Jul 20 16:58:04 2007 UTC (16 years, 9 months ago) by torben
File size: 9288 byte(s)
* SolutionMatrix.{h|cpp}: cleanup clean
* SolutionInt.{h|cpp}: cleanup and finally made rotate90() work correctly
* QueensMain.cpp: minor visual corrections to pop-up menu


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 if (m_solutions != NULL)
134 delete m_solutions;
135 if (m_sol != NULL)
136 delete m_sol;
137 }
138
139 void QueensMain::start()
140 {
141 m_sizeSelector->setEnabled( false );
142 m_start->setEnabled( false );
143 m_stop->setEnabled( true );
144 m_list->clear();
145 m_status->setText( QString("Searching ...") );
146
147 m_board->setMatrix(0);
148
149 if (m_queens != NULL) {
150 m_queens->wait();
151 delete m_queens;
152 m_queens = 0;
153 }
154
155 if (m_solutions != NULL)
156 delete m_solutions;
157 switch (m_sortalgo) {
158 case SortList:
159 m_solutions = new ContainerList(this);
160 break;
161 case SortVector:
162 m_solutions = new ContainerVector(this);
163 break;
164 case SortHash:
165 m_solutions = new ContainerHash(this);
166 break;
167 case SortMNVector:
168 m_solutions = new ContainerMNVector(this);
169 break;
170 }
171
172 if (m_sol != NULL)
173 delete m_sol;
174
175 switch(this->m_storage) {
176 case StorageInt:
177 m_sol = new SolutionInt(m_sizeSelector->value());
178 break;
179 case StorageMatrix:
180 m_sol = new SolutionMatrix(m_sizeSelector->value());
181 break;
182 }
183
184
185 m_elapsed.start();
186 m_time.start();
187 m_queens = new Queens(this, m_sol, m_solutions, m_sizeSelector->value() ,false);
188 m_queens->start();
189 }
190
191 void QueensMain::stop()
192 {
193 m_solutions->halt();
194 m_queens->stop();
195 m_queens->wait();
196
197 int num = m_solutions->numSolutions();
198 m_status->setText( QString("Aborted. Found ") + QString::number(num,10).append(" solutions") );
199 m_list->clear();
200 for (int i=1; i<=num; i++)
201 m_list->insertItem( QString("Solution no ") + QString::number(i,10), i);
202
203 delete m_queens;
204 m_queens = NULL;
205
206 m_list->setEnabled( true );
207 m_sizeSelector->setEnabled( true );
208 m_start->setEnabled( true );
209 m_stop->setEnabled( false );
210 }
211
212 void QueensMain::foundSolution()
213 {
214 int num = m_solutions->numSolutions();
215 if (m_elapsed.elapsed() > 500)
216 {
217 m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") );
218 m_elapsed.restart();
219 }
220 }
221
222 void QueensMain::finishedSearch()
223 {
224 uniqueSolutions();
225 m_sizeSelector->setEnabled( true );
226 m_start->setEnabled( true );
227 m_stop->setEnabled( false );
228 }
229
230
231 void QueensMain::showSolution()
232 {
233 int index = m_list->currentItem();
234 Solution* sol = m_solutions->solution( index );
235 m_board->setMatrix( sol );
236 }
237
238 void QueensMain::resize(int size)
239 {
240 m_board->setSize( size );
241 m_board->repaint( true );
242 m_list->clear();
243 }
244
245 QString QueensMain::elapsed()
246 {
247 int time = m_time.elapsed();
248 int msec = time % 1000;
249 int sec = time / 1000;
250 int min = sec / 60;
251 sec %= 60;
252 QString smsec = (QString::number(msec)).rightJustify(3, '0');
253 QString ssec = (QString::number(sec)).rightJustify(2,'0');
254
255 QString res;
256 if (min!=0)
257 res = QString("%1:%2.%3").arg(min).arg(ssec).arg(smsec);
258 else
259 res = QString("%1.%2").arg(sec).arg(smsec);
260 return res;
261 }
262
263 void QueensMain::uniqueSolutions()
264 {
265 m_totalcount = m_solutions->numSolutions();
266 m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") );
267
268 m_elapsed.restart();
269 m_solutions->uniqueSolutions();
270
271 int uniq = m_solutions->numSolutions();
272
273
274 QString msg;
275 msg = QString( "Found %1 unique solutions, of %2 total solutions. Time elapsed: %3" ).arg(uniq).arg(m_totalcount).arg( elapsed() );
276
277 m_list->clear();
278 for (int i=1; i<=uniq; i++)
279 m_list->insertItem( QString("Unique solution no ") + QString::number(i,10), i);
280
281 m_status->setText( msg );
282 }
283
284 void QueensMain::contextMenuEvent( QContextMenuEvent *event)
285 {
286 QPopupMenu *contextMenu = new QPopupMenu( this );
287 contextMenu->setCheckable( true );
288 QLabel *sortCaption = new QLabel("<b><i>Container class</i></b>", this);
289 sortCaption->setAlignment( Qt::AlignCenter );
290 contextMenu->insertItem( sortCaption );
291
292 contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1);
293 contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2);
294 contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3);
295 contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4);
296
297 QLabel *storageCaption = new QLabel("<b><i>Solution class</i></b>", this);
298 storageCaption->setAlignment( Qt::AlignCenter );
299 contextMenu->insertItem( storageCaption );
300 contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 5);
301 contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 6);
302
303
304 switch (m_sortalgo) {
305 case SortList:
306 contextMenu->setItemChecked( 1, true);
307 break;
308 case SortVector:
309 contextMenu->setItemChecked( 2, true);
310 break;
311 case SortHash:
312 contextMenu->setItemChecked( 3, true);
313 break;
314 case SortMNVector:
315 contextMenu->setItemChecked( 4, true);
316 break;
317 }
318
319 switch (m_storage) {
320 case StorageMatrix:
321 contextMenu->setItemChecked(5, true);
322 break;
323 case StorageInt:
324 contextMenu->setItemChecked(6, true);
325 break;
326 }
327
328
329 contextMenu->exec( event->globalPos() );
330 delete contextMenu;
331 }
332
333 void QueensMain::duplicateRemoved()
334 {
335 if (m_elapsed.elapsed() > 500)
336 {
337 int uniq = m_solutions->getUniqueRemoved();
338 QString status;
339 status = QString ("Sorting %1 solutions. So far identified %2 duplicates").arg(m_totalcount).arg(uniq);
340 m_status->setText(status);
341 m_elapsed.restart();
342 }
343 }
344
345 void QueensMain::sortList()
346 {
347 m_sortalgo = SortList;
348 }
349
350 void QueensMain::sortVector()
351 {
352 m_sortalgo = SortVector;
353 }
354
355 void QueensMain::sortHash()
356 {
357 m_sortalgo = SortHash;
358 }
359
360 void QueensMain::sortMNVector()
361 {
362 m_sortalgo = SortMNVector;
363 }
364
365 void QueensMain::storageInt()
366 {
367 m_storage = StorageInt;
368 }
369
370 void QueensMain::storageMatrix()
371 {
372 m_storage = StorageMatrix;
373 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20