Salome HOME
Merge from V6_main_20120808 08Aug12
[tools/hxx2salome.git] / src / hxx2salome.cpp
1 #include <QtCore/QDir>
2 #include <QtCore/QFile>
3 #include <QtCore/QFileInfo>
4 #include <QtCore/QTextStream>
5 #include <QtGui/QApplication>
6 #include <QtGui/QFileDialog>
7 #include <QtGui/QMessageBox>
8
9 #include <cstdlib>
10
11 #include "hxx2salome.h"
12
13 // VSR: uncomment for debug mode
14 // #define __DEBUG__
15
16 static QString quote( const QString& val )
17 {
18   QString v = val;
19   if ( !v.startsWith( "\"" ) ) v.prepend( "\"" );
20   if ( !v.endsWith( "\"" ) ) v.append( "\"" );
21   return v;
22 }
23
24 static QString unquote( const QString& val )
25 {
26   QString v = val;
27   if ( v.startsWith( "\"" ) ) v = v.remove( 0, 1 );
28   if ( v.endsWith( "\"" ) ) v = v.remove( v.length()-1, 1 );
29   return v;
30 }
31
32 static QString relFileNameFromDir( const QString& dir, const QString& filename )
33 {
34 #ifdef __DEBUG__
35   printf( "0. dir: %s, filename: %s\n", qPrintable( dir ), qPrintable( filename ) );
36 #endif
37   if ( !filename.isEmpty() ) {
38     QString dirpath  = QFileInfo( dir.isEmpty() ? QDir::currentPath() : dir ).absoluteFilePath();
39     QString filepath = QFileInfo( filename ).absoluteFilePath();
40 #ifdef __DEBUG__
41     printf( "1. dirpath: %s, filepath: %s\n", qPrintable( dirpath ), qPrintable( filepath ) );
42 #endif
43     if ( filepath.startsWith( dirpath ) ) {
44       QString fpath = filepath.mid( dirpath.length() );
45       if ( fpath.startsWith( "/" ) ) fpath.remove( 0, 1 );
46 #ifdef __DEBUG__
47       printf( "2. fpath: %s\n", qPrintable( fpath ) );
48 #endif
49       return fpath;
50     }
51   }
52   return filename;
53 }
54
55 HXX2Salome::HXX2Salome() : QDialog()
56 {
57   setupUi( this );
58   retrieve();
59 }
60
61 HXX2Salome::~HXX2Salome()
62 {
63   dump();
64 }
65
66 void HXX2Salome::retrieve()
67 {
68   QFile file( QDir::home().absoluteFilePath( ".hxx2salome" ) );
69   if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
70     QTextStream in( &file );
71     while ( !in.atEnd() ) {
72       QString line = in.readLine();
73       QRegExp re( "^(.*)\\s+(.*)$" );
74       if ( re.exactMatch( line ) ) {
75         QString var = re.cap( 1 ).trimmed();
76         QString val = unquote( re.cap( 2 ).trimmed() );
77         if ( !var.isEmpty() && !val.isEmpty() ) {
78           if ( var == "CppDir" )
79             SourceTreeText->setText( val );
80           else if ( var == "CppInc" )
81             IncludeText->setText( val );
82           else if ( var == "CppLib" )
83             LibraryText->setText( val );
84           else if ( var == "EnvFile" )
85             EnvFileText->setText( val );
86           else if ( var == "SalomeDir" )
87             OutputTreeText->setText( val );
88           else if ( var == "Shell" )
89             ShellChoice->setCurrentIndex( val == "csh" ? 1 : 0 );
90         }
91       }
92     }
93     file.close();
94   }
95 }
96
97 void HXX2Salome::dump()
98 {
99   QFile file( QDir::home().absoluteFilePath( ".hxx2salome" ) );
100   if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) ) {
101     file.write( QString( "CppDir %1\n" ).arg( quote( SourceTreeText->text() ) ).toLatin1() );
102     file.write( QString( "CppInc %1\n" ).arg( quote( IncludeText->text() ) ).toLatin1() );
103     file.write( QString( "CppLib %1\n" ).arg( quote( LibraryText->text() ) ).toLatin1() );
104     file.write( QString( "SalomeDir %1\n" ).arg( quote( OutputTreeText->text() ) ).toLatin1() );
105     file.write( QString( "EnvFile %1\n" ).arg( quote( EnvFileText->text() ) ).toLatin1() );
106     file.write( QString( "Shell %1\n" ).arg( ShellChoice->currentIndex() == 1 ? "csh" : "bash" ).toLatin1() );
107   }
108   file.close();
109 }
110
111 void HXX2Salome::on_CloseButton_clicked()
112 {
113   close();
114 }
115
116 void HXX2Salome::on_SourceTreeButton_clicked()
117 {
118   QString s = QFileDialog::getExistingDirectory( this,
119                                                  tr( "Get Existing directory" ),
120                                                  SourceTreeText->text() ); 
121   if ( !s.isEmpty() ) SourceTreeText->setText( s );
122 }
123
124 void HXX2Salome::on_IncludeButton_clicked()
125 {
126   QString s = QFileDialog::getOpenFileName( this,
127                                             tr( "Choose a file to open" ),
128                                             IncludeText->text(),
129                                             tr( "Include files (*.h *.hh *.hxx *.hpp)" ) );
130   if ( !s.isEmpty() ) {
131     IncludeText->setText( relFileNameFromDir( SourceTreeText->text().trimmed(), s ) );
132   }
133 }
134
135 void HXX2Salome::on_LibraryButton_clicked()
136 {
137   QString s = QFileDialog::getOpenFileName( this,
138                                             tr( "Choose a file to open" ),
139                                             LibraryText->text(),
140                                             tr( "Shared Libraries (*.so *.dll)" ) );
141   if ( !s.isEmpty() ) {
142     LibraryText->setText( relFileNameFromDir( SourceTreeText->text().trimmed(), s ) );
143   }
144 }
145
146 void HXX2Salome::on_EnvFileButton_clicked()
147 {
148   QString s = QFileDialog::getOpenFileName( this,
149                                             tr( "Choose a script file to open" ),
150                                             EnvFileText->text(),
151                                             tr( "Environment files (*.csh *.sh)" ) );
152   if ( !s.isEmpty() ) EnvFileText->setText( s );
153 }
154
155 void HXX2Salome::on_OutputTreeButton_clicked()
156 {
157   QString s = QFileDialog::getExistingDirectory( this,
158                                                  tr( "Choose a directory" ),
159                                                  OutputTreeText->text() ); 
160   if ( !s.isEmpty() ) OutputTreeText->setText( s );
161 }
162
163 void HXX2Salome::on_GenerateButton_clicked()
164 {
165   // check input validity
166   QString CppDir = SourceTreeText->text().trimmed();
167   QFileInfo fid( CppDir.isEmpty() ? QString( "." ) : CppDir );
168
169   if ( CppDir.isEmpty() ) {
170     QMessageBox::StandardButton btn =
171       QMessageBox::warning( this,
172                             tr( "Warning" ),
173                             tr( "You are about to use the current directory for the C++ component tree!\nContinue?" ),
174                             QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );
175     if ( btn != QMessageBox::Yes ) return;
176   }
177
178   QString CppInc = IncludeText->text().trimmed();
179   if ( CppInc.isEmpty() ) {
180     QMessageBox::critical( this,
181                            tr( "Error" ),
182                            tr( "Component C++ include file is not specified!" ) );
183     return;
184   }
185   if ( QFileInfo( CppInc ).isAbsolute() ) CppInc = relFileNameFromDir( CppDir, CppInc );
186   if ( QFileInfo( CppInc ).isAbsolute() ) {
187     QMessageBox::critical( this,
188                            tr( "Error" ),
189                            tr( "Component C++ include file is specified in directory other than\n%1!" ).arg( CppDir ) );
190     return;
191   }
192   CppInc = QFileInfo( CppInc ).fileName();
193
194   QString CppLib = LibraryText->text().trimmed();
195   if ( CppLib.isEmpty() ) {
196     QMessageBox::critical( this,
197                            tr( "Error" ),
198                            tr( "Component shared library is not specified!" ) );
199     return;
200   }
201   if ( QFileInfo( CppLib ).isAbsolute() ) CppLib = relFileNameFromDir( CppDir, CppLib );
202   if ( QFileInfo( CppLib ).isAbsolute() ) {
203     QMessageBox::critical( this,
204                            tr( "Error" ),
205                            tr( "Component shared library is specified in directory other than\n%1!" ).arg( CppDir ) );
206     return;
207   }
208   CppLib = QFileInfo( CppLib ).fileName();
209
210   QString SalomeDir = OutputTreeText->text().trimmed();
211   QFileInfo fis( SalomeDir.isEmpty() ? QString( "." ) : SalomeDir );
212
213   if ( SalomeDir.isEmpty() ) {
214     QMessageBox::StandardButton btn =
215       QMessageBox::warning( this,
216                             tr( "Warning" ),
217                             tr( "You are about to use the current directory as the Salome component tree!\nContinue?" ),
218                             QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );
219     if ( btn != QMessageBox::Yes ) return;
220   }
221
222   QString EnvFile = EnvFileText->text().trimmed();
223   QFileInfo fienv( EnvFile );
224
225   // generate command line
226   QStringList cmdlist;
227   cmdlist << "${HXX2SALOME_ROOT_DIR}/hxx2salome";
228   if ( MakeGUI->isChecked() )
229     cmdlist << "-g";
230   if ( Compile->isChecked() )
231     cmdlist << "-c";
232   if ( ShellChoice->currentIndex() == 1 )
233     cmdlist << "-s csh";
234   if ( !EnvFile.isEmpty() ) {
235     cmdlist << "-e";
236     cmdlist << quote( fienv.absoluteFilePath() );
237   }
238   cmdlist << quote( fid.absoluteFilePath() );
239   cmdlist << quote( CppInc );
240   cmdlist << quote( CppLib );
241   cmdlist << quote( fis.absoluteFilePath() );
242   QString command = cmdlist.join( " " );
243
244   // execute command
245 #ifdef __DEBUG__
246   printf( "command: %s\n", qPrintable( command ) );
247 #endif
248   QApplication::setOverrideCursor( Qt::WaitCursor );
249   std::system( command.toLatin1().constData() );
250   QApplication::restoreOverrideCursor();
251 }