Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[tools/hxx2salome.git] / src / hxx2salome.ui.h
1 /****************************************************************************
2 ** ui.h extension file, included from the uic-generated form implementation.
3 **
4 ** If you want to add, delete, or rename functions or slots, use
5 ** Qt Designer to update this file, preserving your code.
6 **
7 ** You should not define a constructor or destructor in this file.
8 ** Instead, write your code in functions called init() and destroy().
9 ** These will automatically be called by the form's constructor and
10 ** destructor.
11 *****************************************************************************/
12
13 #include <iostream>
14 #include <fstream>
15 #include <string>
16 #include <cstdlib>
17 #include <libgen.h>
18 #include <qfiledialog.h>
19
20 static
21 inline void extractDirNameBaseName(const QString & name, 
22                             QString & dir, QString & base )
23 {
24     if (name != "") {
25         char *s;
26         s = strdup(name.latin1());
27         dir = dirname(s);
28         dir += "/";
29         strcpy(s, name.latin1());
30         base = basename(s);
31         free(s);
32     }
33     else {
34         base = "";
35         dir = "";
36     }
37 }
38
39 static 
40 inline void extract_value( QString & Tq)
41 {
42     std::string t = Tq.latin1();
43     std::string::size_type p1, p2;
44     std::string spaces = " \t\n\r";
45     
46     p1 = t.find_first_of('"');
47     if (p1 == std::string::npos) p1 = 0;
48     else p1++;
49     while ((p1 < t.length()) && 
50            (spaces.find(t[p1]) != std::string::npos))
51         p1++;
52                 
53     p2 = t.find_last_of('"');
54     if (p2 == std::string::npos) p2 = t.length();
55     else p2--;
56     while ((p2 >= 0) && 
57            (spaces.find(t[p2]) != std::string::npos))
58         p2--;
59     
60     if (p1 <= p2)
61         t = t.substr(p1, p2-p1+1);
62     else
63         t = "";
64     Tq = t.c_str();
65 }
66
67 void HXX2Salome::CloseButton_pressed()
68 {
69     close();
70 }
71
72
73 void HXX2Salome::SourceTreeButton_pressed()
74 {
75     QString s = QFileDialog::getExistingDirectory(
76             CppDir,
77             this,
78             "get existing directory",
79             "Choose a directory",
80             TRUE ); 
81     if (!s.isEmpty()) {
82         CppDir = s;
83         SourceTreeText->setText(s);
84     }
85 }
86
87
88 void HXX2Salome::newComponentTree()
89 {
90     QString s = SourceTreeText->text();
91     if (!s.isEmpty()) CppDir = s;
92 }
93
94
95 void HXX2Salome::newIncludeFile()
96 {
97     QString s = IncludeText->text();
98     QString dir, base;
99     
100     if (!s.isEmpty()) {
101         extractDirNameBaseName(s, dir, base);
102         CppInc = base;
103         if (dir != "./")
104             CppIncDir = dir;
105     }
106 }
107
108
109 void HXX2Salome::newSharedLibrary()
110 {
111     QString s = LibraryText->text();
112     QString dir, base;
113     
114     if (!s.isEmpty()) {
115         extractDirNameBaseName(s, dir, base);
116         CppLib = base;
117         if (dir != "./")
118             CppLibDir = dir;
119     }
120 }
121
122
123 void HXX2Salome::newEnvFile()
124 {
125     QString s = EnvFileText->text();
126     if (!s.isEmpty()) EnvFile = s;
127 }
128
129
130 void HXX2Salome::newOutputTree()
131 {
132     QString s = OutputTreeText->text();
133     if (!s.isEmpty()) SalomeDir = s;
134     
135 }
136
137
138 void HXX2Salome::OutputTreeButton_pressed()
139 {
140     QString s = QFileDialog::getExistingDirectory(
141             SalomeDir,
142             this,
143             "get directory",
144             "Choose a directory",
145             TRUE ); 
146     if (!s.isEmpty()) {
147         SalomeDir = s;
148         OutputTreeText->setText(s);
149     }
150 }
151
152 void HXX2Salome::IncludeButton_pressed()
153 {
154     QString s = QFileDialog::getOpenFileName(
155             CppIncDir + CppInc,
156             "Include files (*.h *.hh *.hxx *.hpp)",
157             this,
158             "C++ include file",
159             "Choose a file to open" );
160     if (!s.isEmpty()) {
161         extractDirNameBaseName(s, CppIncDir, CppInc);
162         IncludeText->setText(CppInc);
163     }
164 }
165
166
167 void HXX2Salome::LibraryButton_pressed()
168 {
169     QString s = QFileDialog::getOpenFileName(
170             CppLibDir + CppLib,
171             "Shared Libraries (*.so *.dll)",
172             this,
173             "Library file",
174             "Choose a file to open" );
175     if (!s.isEmpty()) {
176         extractDirNameBaseName(s, CppLibDir, CppLib);
177         LibraryText->setText(CppLib);
178     }
179 }
180
181
182 void HXX2Salome::EnvFileButton_pressed()
183 {
184     QString s = QFileDialog::getSaveFileName(
185             EnvFile,
186             "Environment files (*.csh *.sh)",
187             this,
188             "Environment file",
189             "Choose a script file to open" );
190     if (!s.isEmpty()) {
191         EnvFile = s;
192         EnvFileText->setText(s);
193     }
194     
195 }
196
197
198 void HXX2Salome::shellChanged(int n)
199 {
200 }
201
202
203 void HXX2Salome::GenerateButton_pressed()
204 {
205     QString command;
206     command = "$HXX2SALOME_ROOT_DIR/hxx2salome ";
207     if (makeGUI->isChecked())
208         command += " \"-g\" ";
209     if (Compile->isChecked())
210         command += " \"-c\" ";
211     if (shell == 1)
212         command += " -s csh ";
213     if (!EnvFile.isEmpty()) {
214         command += " -e ";
215         command += EnvFile;
216         command += " ";
217     }
218     command += CppDir;
219     command += " \"";
220     command += CppInc;
221     command += "\" \"";
222     command += CppLib;
223     command += "\" \"";
224     command += SalomeDir;
225     command += "\"";
226   
227     std::cerr << "command = " << command << std::endl;
228     QCursor QOld(Qt::ArrowCursor), QNew(Qt::WaitCursor);
229     this->setCursor(QNew);
230     std::system(command);
231     this->setCursor(QOld);
232 }
233
234 void HXX2Salome::init()
235 {   
236     std::cerr << "HXX2Salome::init" << std::endl;
237     shell = 0;
238     CppDir = "";
239     CppInc = "";
240     CppLib = "";
241     EnvFile = "";
242     SalomeDir = "";
243         
244     char *home = getenv("HOME");
245     std::string s(home), t;
246     QString Tq;
247     s += "/.hxx2salome";
248     std::ifstream f(s.c_str());
249     while (f.good()) {
250         
251         f >> s;
252         getline(f, t);
253         Tq = t.c_str();
254         extract_value(Tq);
255         if (s == "CppDir") {
256             CppDir = Tq;
257             SourceTreeText->setText(CppDir);
258         }
259         else if (s == "CppInc") {
260             extractDirNameBaseName(Tq, CppIncDir, CppInc);
261             IncludeText->setText(CppInc);
262         }
263         else if (s == "CppLib") {
264             extractDirNameBaseName(Tq, CppLibDir, CppLib);
265             LibraryText->setText(CppLib);
266         }
267         else if (s == "EnvFile") {
268             EnvFile = Tq; 
269             EnvFileText->setText(EnvFile);
270         }
271         else if (s == "SalomeDir") {
272             SalomeDir = Tq;
273             OutputTreeText->setText(SalomeDir);
274         }
275         else if (s == "Shell") {
276             if (Tq == "bash") shell = 0;
277             else if (Tq == "csh") shell = 1;
278             ShellChoice->setCurrentItem(shell);
279             ShellChoice->setCurrentText(Tq);
280         }        
281         if (f.eof()) break;
282     }
283     
284     f.close();
285 }
286
287
288 void HXX2Salome::ShellChoice_activated( int n)
289 {
290     shell = ShellChoice->currentItem();
291 }
292
293
294 void HXX2Salome::destroy()
295 {
296     std::cerr << "HXX2Salome::destroy" << std::endl;
297     char *home = getenv("HOME");
298     std::string s(home);
299     s += "/.hxx2salome";
300     std::ofstream f(s.c_str());
301     f << "CppDir " << "\"" << CppDir << "\"" << std::endl;
302     f << "CppInc " << "\"" << CppIncDir << CppInc << "\"" << std::endl;
303     f << "CppLib " << "\"" << CppLibDir << CppLib << "\"" << std::endl;
304     f << "SalomeDir " << "\"" << SalomeDir << "\"" << std::endl;
305     f << "EnvFile " << "\"" << EnvFile << "\"" << std::endl;
306     f << "Shell ";
307     switch (shell) {
308     case 0 : f<< "bash";
309         break;
310     case 1: f << "csh";
311         break;
312     default: f << "bash";
313         break;
314     }
315     f << std::endl;
316     f.close();
317 }
318