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

Contents of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download)
Thu Jul 19 21:34:15 2007 UTC (16 years, 10 months ago) by torben
File size: 8138 byte(s)
Initial import


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20