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

Annotation of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (hide annotations) (download)
Fri Jul 20 09:54:09 2007 UTC (16 years, 10 months ago) by torben
File size: 9134 byte(s)
No need for config control of this IDE-generated file


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20