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

Annotation of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 327 - (hide annotations) (download)
Wed Sep 16 18:53:22 2009 UTC (14 years, 8 months ago) by torben
File size: 9541 byte(s)
Compile with qt 4.5

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20