7daef3ef672d758c38559eabd83cb237e52b6ecc
[snark14.git] / tools / Input / basis.cpp
1 /** @file basis.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 /* basis.cpp
9    This file is part of the snark14Input package
10    Written by Bruno M. Carvalho and Deniz Sarioz
11    licensed under QPLv1.0 which should accompany this distribution in the file 'QPL'
12 */ 
13
14 #include "basis.h"
15 #include "misc.h"
16 #include "variables.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <errno.h>
20
21 #include <qcheckbox.h>
22 #include <qradiobutton.h>
23 #include <qframe.h>
24 #include <qlabel.h>
25 #include <qlineedit.h>
26 #include <qpushbutton.h>
27 #include <qbuttongroup.h>
28 #include <qradiobutton.h>
29 #include <qlayout.h>
30 #include <qvariant.h>
31 #include <qtooltip.h>
32 #include <qwhatsthis.h>
33 #include <qmessagebox.h>
34
35 /** 
36  *  Constructs a basiswindow which is a child of 'parent', with the 
37  *  name 'name' and widget flags set to 'f' 
38  *
39  *  The dialog will by default be modeless, unless you set 'modal' to
40  *  TRUE to construct a modal dialog.
41 @param void
42 @author Bruno M. Carvalho
43 @version 1.0 */
44 basiswindow::basiswindow( QWidget* parent,  const char* name, bool modal, WFlags fl )
45     : QDialog( parent, name, modal, fl )
46 {
47     if ( !name )
48         setName( "basiswindow" );
49     resize( 400, 300 );
50     setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, sizePolicy().hasHeightForWidth() ) );
51     setMinimumSize( QSize( 400, 300 ) );
52     setMaximumSize( QSize( 400, 300 ) );
53     setCaption( tr( "Basis Window" ) );
54
55     basislabel = new QLabel( this, "basislabel" );
56     basislabel->setGeometry( QRect( 10, 10, 120, 30 ) ); 
57     basislabel->setText( tr( "BASIS" ) );
58
59     basisbuttongroup = new QButtonGroup( this, "basisbuttongroup" );
60     basisbuttongroup->setGeometry( QRect( 20, 40, 360, 170 ) ); 
61     basisbuttongroup->setTitle( tr( "basis function" ) );
62     basisbuttongroup->setAlignment( int( QButtonGroup::AlignHCenter ) );
63
64     pixelbutton = new QRadioButton( basisbuttongroup, "pixelbutton" );
65     pixelbutton->setGeometry( QRect( 10, 20, 120, 30 ) ); 
66     pixelbutton->setText( tr( "PIXEL" ) ); 
67     pixelbutton->setChecked(true);
68     QObject::connect(pixelbutton,SIGNAL(clicked()),this,SLOT(deactivateBlobFields()));
69
70     blobbutton = new QRadioButton( basisbuttongroup, "blobbutton" );
71     blobbutton->setGeometry( QRect( 10, 70, 80, 30 ) ); 
72     blobbutton->setText( tr( "BLOB" ) ); 
73     QObject::connect(blobbutton,SIGNAL(clicked()),this,SLOT(activateBlobFields()));
74
75     // "paragraph" ++ by deniz
76     useblobdefaultsbox = new QCheckBox( basisbuttongroup, "useblobdefaultsbox" );
77     useblobdefaultsbox->setGeometry( QRect( 10, 115, 135, 30 ) );
78     useblobdefaultsbox->setText( tr( "use blob defaults" ) );
79     useblobdefaultsbox->setEnabled(FALSE);
80     //    useblobdefaultsbox->setChecked(false);
81     QObject::connect(useblobdefaultsbox,SIGNAL(clicked()),this,SLOT(deactivateBlobFields()));
82
83     supportlabel = new QLabel( basisbuttongroup, "supportlabel" );
84     supportlabel->setGeometry( QRect( 105, 50, 60, 20 ) ); 
85     supportlabel->setText( tr( "support" ) ); 
86     supportlabel->setEnabled(FALSE);
87
88     support = new QLineEdit( basisbuttongroup, "support" );
89     support->setGeometry( QRect( 90, 70, 70, 30 ) ); 
90     support->setEnabled(FALSE);
91
92     shapelabel = new QLabel( basisbuttongroup, "shapelabel" );
93     shapelabel->setEnabled( FALSE );
94     shapelabel->setGeometry( QRect( 185, 50, 60, 20 ) ); 
95     shapelabel->setText( tr( "shape" ) );
96
97     shape = new QLineEdit( basisbuttongroup, "shape" );
98     shape->setGeometry( QRect(170, 70, 70, 30 ) ); 
99     shape->setEnabled(FALSE);
100
101     deltalabel = new QLabel( basisbuttongroup, "deltalabel" );
102     deltalabel->setEnabled( FALSE );
103     deltalabel->setGeometry( QRect( 265, 50, 60, 20 ) ); 
104     deltalabel->setText( tr( "delta" ) );
105
106     delta = new QLineEdit( basisbuttongroup, "delta" );
107     delta->setGeometry( QRect( 250, 70, 70, 30 ) ); 
108     delta->setEnabled(FALSE);
109
110     Line9 = new QFrame( this, "Line9" );
111     Line9->setGeometry( QRect( -10, 215, 420, 16 ) ); 
112     Line9->setFrameStyle( QFrame::HLine | QFrame::Sunken );
113
114     okbutton = new QPushButton( this, "okbutton" );
115     okbutton->setGeometry( QRect( 50, 230, 100, 40 ) ); 
116     okbutton->setText( tr( "OK" ) );
117     QObject::connect(okbutton,SIGNAL(clicked()),this,SLOT(checkValues()));
118
119     cancelbutton = new QPushButton( this, "cancelbutton" );
120     cancelbutton->setGeometry( QRect( 250, 230, 100, 40 ) ); 
121     cancelbutton->setText( tr( "Cancel" ) );
122     QObject::connect(cancelbutton,SIGNAL(clicked()),this,SLOT(reject()));
123 }
124
125 /**  
126  *  Destroys the object and frees any allocated resources
127  */
128 basiswindow::~basiswindow()
129 {
130     // no need to delete child widgets, Qt does it all for us
131 }
132
133 /** Activates blob subfields 
134 @param void
135 @author Bruno M. Carvalho
136 @version 1.0 */
137 void basiswindow::activateBlobFields()
138 {
139   if(blobbutton->isChecked()) {
140     useblobdefaultsbox->setEnabled(TRUE);
141     useblobdefaultsbox->setChecked(false);
142     support->setEnabled(TRUE);
143     supportlabel->setEnabled(TRUE);
144     shape->setEnabled(TRUE);
145     shapelabel->setEnabled(TRUE);
146     delta->setEnabled(TRUE);
147     deltalabel->setEnabled(TRUE);
148   }
149   else {
150     useblobdefaultsbox->setEnabled(FALSE);
151     support->setEnabled(FALSE);
152     supportlabel->setEnabled(FALSE);
153     shape->setEnabled(FALSE);
154     shapelabel->setEnabled(FALSE);
155     delta->setEnabled(FALSE);
156     deltalabel->setEnabled(FALSE);
157   }
158 }
159
160 /** Dectivates blob subfields 
161 @param void
162 @author Bruno M. Carvalho
163 @version 1.0 */
164 void basiswindow::deactivateBlobFields()
165 {
166   // played with by deniz
167   if(pixelbutton->isChecked()) {
168     useblobdefaultsbox->setEnabled(FALSE);
169     support->setEnabled(FALSE);
170     supportlabel->setEnabled(FALSE);
171     shape->setEnabled(FALSE);
172     shapelabel->setEnabled(FALSE);
173     delta->setEnabled(FALSE);
174     deltalabel->setEnabled(FALSE);
175   } else if(useblobdefaultsbox->isChecked()) {
176     support->setEnabled(FALSE);
177     supportlabel->setEnabled(FALSE);
178     shape->setEnabled(FALSE);
179     shapelabel->setEnabled(FALSE);
180     delta->setEnabled(FALSE);
181     deltalabel->setEnabled(FALSE);
182   } else activateBlobFields();
183 }
184
185 /** Returns a string for BASIS input sequence
186 @param void
187 @return basis
188 @author Bruno M. Carvalho
189 @version 1.0 */
190 QString basiswindow::getOutput()
191 {
192   QString output;
193
194   output.sprintf("BASIS ");
195   if(blobbutton->isChecked()) {
196     // ++ by deniz
197     if(useblobdefaultsbox->isChecked()) { 
198       output.append("BLOBS ");
199     } else {
200       output.append("BLOB ");
201       output+=support->text();
202       output.append(" ");
203       output+=shape->text();
204       output.append(" ");
205       output+=delta->text();
206       output.append(" ");
207     }
208   }
209   else
210     if(pixelbutton->isChecked()) {
211       output.append("PIXEL ");
212     }
213   //output.append("\n");
214
215   return output;
216 }
217
218 void basiswindow::checkValues()
219 {
220   int f=0;
221   float su=0,sh=0,d=0;
222   
223   if((blobbutton->isChecked()) && (!(useblobdefaultsbox->isChecked()))) {
224     su=strtof((const char *) (support->text().lower()) ); // lower: bug#148
225     if(errnum) 
226       f=1; else {
227         if(su<=0)
228           f=4;
229         sh=strtof((const char *) (shape->text().lower()) );
230         if(errnum) 
231           f=2; else {
232             if(sh<0)
233               f=5;
234             d=strtof((const char *) (delta->text().lower()) );
235             if(errnum) 
236               f=3; else {
237                 if(d<=0)
238                   f=6;
239               }
240           }
241       }
242   }
243   switch(f) {
244   case 0: accept();
245     break;
246   case 1: QMessageBox::information(this,"SnarkInput","Error!\n"
247                                    "Invalid support format.");
248     break;
249   case 2: QMessageBox::information(this,"SnarkInput","Error!\n"
250                                    "Invalid shape format.");
251     break;
252   case 3: QMessageBox::information(this,"SnarkInput","Error!\n"
253                                    "Invalid delta format.");
254     break;
255   case 4: QMessageBox::information(this,"SnarkInput","Error!\n"
256                                    "support must be positive.");
257     break;
258   case 5: QMessageBox::information(this,"SnarkInput","Error!\n"
259                                    "shape must be nonnegative.");
260     break;
261   case 6: QMessageBox::information(this,"SnarkInput","Error!\n"
262                                    "delta must be positive.");
263     break;
264   }
265 }
266