Fixed text file permissions
[snark14.git] / tools / Input / stop.cpp
1 /** @file stop.cpp
2  @package snark14Input
3  @author Bruno M. Carvalho and Deniz Sarioz
4  licensed under (open-source) QPL v1.0
5  which accompanies this distribution in the file QPL
6  */
7
8 #include "stop.h"
9 #include "variables.h"
10 #include "misc.h"
11 #include <stdio.h>
12
13 #include <qbuttongroup.h>
14 #include <qframe.h>
15 #include <qlabel.h>
16 #include <qcombobox.h>
17 #include <qlineedit.h>
18 #include <qpushbutton.h>
19 #include <qradiobutton.h>
20 #include <qlayout.h>
21 #include <qvariant.h>
22 #include <qtooltip.h>
23 #include <qwhatsthis.h>
24 #include <qmessagebox.h>
25
26 /** 
27  *  Constructs a stopwindow which is a child of 'parent', with the 
28  *  name 'name' and widget flags set to 'f' 
29  *
30  *  The dialog will by default be modeless, unless you set 'modal' to
31  *  TRUE to construct a modal dialog.
32  @param void
33  @author Bruno M. Carvalho
34  @version 1.0 */
35 stopwindow::stopwindow(QWidget* parent, const char* name, bool modal, WFlags fl) :
36                 QDialog(parent, name, modal, fl)
37 {
38         if (!name)
39                 setName("stopwindow");
40         resize(600, 340);
41         setSizePolicy(
42                         QSizePolicy((QSizePolicy::SizeType) 0, (QSizePolicy::SizeType) 0,
43                                         sizePolicy().hasHeightForWidth()));
44         setMinimumSize(QSize(600, 340));
45         setMaximumSize(QSize(600, 340));
46         setCaption(tr("Stop Window"));
47
48         stoplabel = new QLabel(this, "stoplabel");
49         stoplabel->setGeometry(QRect(10, 10, 70, 30));
50         stoplabel->setSizePolicy(
51                         QSizePolicy((QSizePolicy::SizeType) 0, (QSizePolicy::SizeType) 0,
52                                         stoplabel->sizePolicy().hasHeightForWidth()));
53         stoplabel->setMinimumSize(QSize(70, 30));
54         stoplabel->setMaximumSize(QSize(70, 30));
55         stoplabel->setText(tr("STOP"));
56
57         stopbuttongroup = new QButtonGroup(this, "stopbuttongroup");
58         stopbuttongroup->setGeometry(QRect(15, 45, 575, 205));
59         stopbuttongroup->setTitle(tr("type"));
60         stopbuttongroup->setAlignment(int(QButtonGroup::AlignHCenter));
61
62         iterationbutton = new QRadioButton(stopbuttongroup, "iterationbutton");
63         iterationbutton->setGeometry(QRect(10, 45, 118, 24));
64         iterationbutton->setText(tr("ITERATION"));
65         QObject::connect(iterationbutton, SIGNAL(clicked()), this,
66                         SLOT(activateIterationFields()));
67
68         terminationbutton = new QRadioButton(stopbuttongroup, "terminationbutton");
69         terminationbutton->setGeometry(QRect(10, 100, 135, 24));
70         terminationbutton->setText(tr("TERMINATION"));
71         QObject::connect(terminationbutton, SIGNAL(clicked()), this,
72                         SLOT(activateTerminationFields()));
73
74         nonebutton = new QRadioButton(stopbuttongroup, "nonebutton");
75         nonebutton->setGeometry(QRect(10, 150, 75, 24));
76         nonebutton->setText(tr("NONE"));
77         QObject::connect(nonebutton, SIGNAL(clicked()), this,
78                         SLOT(deactivateFields()));
79
80         iter = new QLineEdit(stopbuttongroup, "iter");
81         iter->setGeometry(QRect(150, 40, 80, 30));
82         iter->setSizePolicy(
83                         QSizePolicy((QSizePolicy::SizeType) 0, (QSizePolicy::SizeType) 0,
84                                         iter->sizePolicy().hasHeightForWidth()));
85         iter->setEnabled(FALSE);
86
87         iterlabel = new QLabel(stopbuttongroup, "iterlabel");
88         iterlabel->setGeometry(QRect(180, 20, 40, 20));
89         iterlabel->setText(tr("iter"));
90         iterlabel->setEnabled(FALSE);
91
92         testname = new QComboBox(stopbuttongroup, "testname");
93         testname->setGeometry(QRect(150, 100, 130, 30));
94         testname->setSizePolicy(
95                         QSizePolicy((QSizePolicy::SizeType) 0, (QSizePolicy::SizeType) 0,
96                                         testname->sizePolicy().hasHeightForWidth()));
97         testname->insertItem("VARIANCE", -1);
98         testname->insertItem("MLST", -1);
99         testname->insertItem("KLDS", -1);
100         testname->insertItem("RESI", -1);
101         testname->insertItem("WSQD", -1);
102         testname->insertItem("TRM1", -1);
103         testname->insertItem("TRM2", -1);
104         testname->setEnabled(FALSE);
105         QObject::connect(testname,SIGNAL(activated(int)),this,SLOT(setEpsilon()));
106
107         testnamelabel = new QLabel(stopbuttongroup, "testnamelabel");
108         testnamelabel->setGeometry(QRect(175, 80, 80, 20));
109         testnamelabel->setText(tr("test_name"));
110         testnamelabel->setEnabled(FALSE);
111
112         epsilon = new QLineEdit(stopbuttongroup, "epsilon");
113         epsilon->setGeometry(QRect(290, 100, 80, 30));
114         epsilon->setSizePolicy(
115                         QSizePolicy((QSizePolicy::SizeType) 0, (QSizePolicy::SizeType) 0,
116                                         epsilon->sizePolicy().hasHeightForWidth()));
117         epsilon->setEnabled(FALSE);
118
119         epsilonlabel = new QLabel(stopbuttongroup, "epsilonlabel");
120         epsilonlabel->setGeometry(QRect(305, 80, 60, 20));
121         epsilonlabel->setText(tr("epsilon"));
122         epsilonlabel->setEnabled(FALSE);
123
124         rprtcb = new QCheckBox( this, "rprtcb" );
125         rprtcb->setGeometry(QRect(400, 145, 80, 30));
126         rprtcb->setText( "RPRT" );
127         rprtcb->setChecked(false);
128         rprtcb->setEnabled(FALSE);
129         QObject::connect(rprtcb,SIGNAL(clicked()),this,SLOT(setSkips()));
130
131         skips = new QLineEdit(stopbuttongroup, "skips");
132         skips->setGeometry(QRect(470, 100, 80, 30));
133         skips->setSizePolicy(
134                         QSizePolicy((QSizePolicy::SizeType) 0, (QSizePolicy::SizeType) 0,
135                                         skips->sizePolicy().hasHeightForWidth()));
136         skips->setEnabled(FALSE);
137
138         skipslabel = new QLabel(stopbuttongroup, "skipslabel");
139         skipslabel->setGeometry(QRect(485, 80, 60, 20));
140         skipslabel->setText(tr("skips"));
141         skipslabel->setEnabled(FALSE);
142
143         Line17 = new QFrame(this, "Line17");
144         Line17->setGeometry(QRect(-5, 265, 415, 16));
145         Line17->setFrameStyle(QFrame::HLine | QFrame::Sunken);
146
147         okbutton = new QPushButton(this, "okbutton");
148         okbutton->setGeometry(QRect(50, 290, 100, 40));
149         okbutton->setText(tr("OK"));
150         QObject::connect(okbutton, SIGNAL(clicked()), this, SLOT(checkValues()));
151
152         cancelbutton = new QPushButton(this, "cancelbutton");
153         cancelbutton->setGeometry(QRect(280, 290, 100, 40));
154         cancelbutton->setText(tr("Cancel"));
155         QObject::connect(cancelbutton, SIGNAL(clicked()), this, SLOT(reject()));
156 }
157
158 /**  
159  *  Destroys the object and frees any allocated resources
160  */
161 stopwindow::~stopwindow()
162 {
163         // no need to delete child widgets, Qt does it all for us
164 }
165
166 /** Activates iteration fields 
167  @param void
168  @author Bruno M. Carvalho
169  @version 1.0 */
170 void stopwindow::activateIterationFields()
171 {
172         if (iterationbutton->isChecked())
173         {
174                 iter->setEnabled(TRUE);
175                 iterlabel->setEnabled(TRUE);
176                 testname->setEnabled(FALSE);
177                 testnamelabel->setEnabled(FALSE);
178                 epsilon->setEnabled(FALSE);
179                 epsilonlabel->setEnabled(FALSE);
180         }
181         else
182         {
183                 iter->setEnabled(FALSE);
184                 iterlabel->setEnabled(FALSE);
185         }
186 }
187
188 /** Activates termination fields 
189  @param void
190  @author Bruno M. Carvalho
191  @version 1.0 */
192 void stopwindow::activateTerminationFields()
193 {
194         if (terminationbutton->isChecked())
195         {
196                 testname->setEnabled(TRUE);
197                 testnamelabel->setEnabled(TRUE);
198                 iter->setEnabled(FALSE);
199                 iterlabel->setEnabled(FALSE);
200                 setEpsilon();
201         }
202         else
203         {
204                 testname->setEnabled(FALSE);
205                 epsilon->setEnabled(FALSE);
206                 epsilonlabel->setEnabled(FALSE);
207                 testnamelabel->setEnabled(FALSE);
208         }
209 }
210
211 /** Deactivates iteration and termination subfields 
212  @param void
213  @author Bruno M. Carvalho
214  @version 1.0 */
215 void stopwindow::deactivateFields()
216 {
217         if (nonebutton->isChecked())
218         {
219                 iter->setEnabled(FALSE);
220                 iterlabel->setEnabled(FALSE);
221                 testname->setEnabled(FALSE);
222                 epsilon->setEnabled(FALSE);
223                 epsilonlabel->setEnabled(FALSE);
224                 testnamelabel->setEnabled(FALSE);
225         }
226         else
227         {
228                 iter->setEnabled(TRUE);
229                 iterlabel->setEnabled(TRUE);
230                 testname->setEnabled(TRUE);
231                 testnamelabel->setEnabled(TRUE);
232                 setEpsilon();
233         }
234 }
235
236 void stopwindow::setEpsilon()
237 {
238         if (testname->currentText().contains("VARIANCE") || testname->currentText().contains("KLDS") || testname->currentText().contains("RESI") || testname->currentText().contains("WSQD"))
239         {
240                 epsilon->setEnabled(TRUE);
241                 epsilonlabel->setEnabled(TRUE);
242         }
243         else
244         {
245                 epsilon->setEnabled(FALSE);
246                 epsilonlabel->setEnabled(FALSE);
247         }
248
249         if (testname->currentText().contains("MLST") || testname->currentText().contains("KLDS") || testname->currentText().contains("RESI") || testname->currentText().contains("WSQD"))
250         {
251                 rprtcb->setEnabled(TRUE);
252         }
253         else
254         {
255                 rprtcb->setEnabled(FALSE);
256         }
257 }
258
259 void stopwindow::setSkips()
260 {
261         if (rprtcb->isChecked())
262         {
263                 skips->setEnabled(TRUE);
264                 skipslabel->setEnabled(TRUE);
265         }
266         else
267         {
268                 skips->setEnabled(FALSE);
269                 skipslabel->setEnabled(FALSE);
270         }
271 }
272
273 /** Returns a string for STOP input sequence
274  @param void
275  @return stop
276  @author Bruno M. Carvalho
277  @version 1.0 */
278 QString stopwindow::getOutput()
279 {
280         QString output;
281
282         output.sprintf("STOP ");
283         if (iterationbutton->isChecked())
284         {
285                 output.append("ITERATION ");
286                 output += iter->text();
287                 output.append(" ");
288         }
289         if (terminationbutton->isChecked())
290         {
291                 output.append("TERMINATION ");
292                 switch (testname->currentItem())
293                 {
294                 case 0:
295                         output.append("VARIANCE");
296                         break;
297                 case 1:
298                         output.append("MLST");
299                         break;
300                 case 2:
301                         output.append("KLDS");
302                         break;
303                 case 3:
304                         output.append("RESI");
305                         break;
306                 case 4:
307                         output.append("WSQD");
308                         break;
309                 case 5:
310                         output.append("TRM1");
311                         break;
312                 case 6:
313                         output.append("TRM2");
314                         break;
315                 }
316                 output.append(" ");
317                 if (testname->currentText().contains("VARIANCE") || testname->currentText().contains("KLDS") || testname->currentText().contains("RESI") || testname->currentText().contains("WSQD"))
318                 {
319                         output += epsilon->text();
320                 }
321
322                 if (rprtcb->isChecked())
323                 {
324                         output.append(" RPRT");
325
326                         float s = strtof((const char *) skips->text());
327                         if (s>1)
328                         {
329                                 output.append(" ");
330                                 output += skips->text();
331                         }
332                 }
333         }
334
335         return output;
336 }
337
338 void stopwindow::checkValues()
339 {
340         int f = 0, i = 0;
341         float e = 0;
342         char str1[5];
343
344         str1[4] = 0;
345         if (iterationbutton->isChecked())
346         {
347                 i = strtoi((const char *) iter->text());
348                 if (errnum)
349                         f = 1;
350                 if (i < 1)
351                         f = 2;
352         }
353         if (terminationbutton->isChecked())
354         {
355                 if (testname->currentText().contains("VARIANCE") || testname->currentText().contains("KLDS") || testname->currentText().contains("RESI") || testname->currentText().contains("WSQD"))
356                 {
357                         e = strtof((const char *) epsilon->text());
358                         if (errnum)
359                                 f = 3;
360                         if (e <= 0)
361                                 f = 4;
362                 }
363         }
364
365         switch (f)
366         {
367         case 0:
368                 accept();
369                 break;
370         case 1:
371                 QMessageBox::information(this, "SnarkInput", "Error!\n"
372                                 "Invalid iter format.");
373                 iter->setFocus();
374                 break;
375         case 2:
376                 QMessageBox::information(this, "SnarkInput", "Error!\n"
377                                 "iter must be greater than 0.");
378                 iter->setFocus();
379                 break;
380         case 3:
381                 QMessageBox::information(this, "SnarkInput", "Error!\n"
382                                 "Invalid epsilon format.");
383                 epsilon->setFocus();
384                 break;
385         case 4:
386                 QMessageBox::information(this, "SnarkInput", "Error!\n"
387                                 "epsilon must be greater than 0.");
388                 epsilon->setFocus();
389                 break;
390         }
391 }