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

Annotation of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations) (download)
Thu Jul 19 22:26:42 2007 UTC (16 years, 10 months ago) by torben
File size: 9099 byte(s)
First steps towards enabling multiple solution types...

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20