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