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