2 #include <QtCore/QFile>
3 #include <QtCore/QFileInfo>
4 #include <QtCore/QTextStream>
5 #include <QtGui/QApplication>
6 #include <QtGui/QFileDialog>
7 #include <QtGui/QMessageBox>
11 #include "hxx2salome.h"
13 // VSR: uncomment for debug mode
16 static QString quote( const QString& val )
19 if ( !v.startsWith( "\"" ) ) v.prepend( "\"" );
20 if ( !v.endsWith( "\"" ) ) v.append( "\"" );
24 static QString unquote( const QString& val )
27 if ( v.startsWith( "\"" ) ) v = v.remove( 0, 1 );
28 if ( v.endsWith( "\"" ) ) v = v.remove( v.length()-1, 1 );
32 static QString relFileNameFromDir( const QString& dir, const QString& filename )
35 printf( "0. dir: %s, filename: %s\n", qPrintable( dir ), qPrintable( filename ) );
37 if ( !filename.isEmpty() ) {
38 QString dirpath = QFileInfo( dir.isEmpty() ? QDir::currentPath() : dir ).absoluteFilePath();
39 QString filepath = QFileInfo( filename ).absoluteFilePath();
41 printf( "1. dirpath: %s, filepath: %s\n", qPrintable( dirpath ), qPrintable( filepath ) );
43 if ( filepath.startsWith( dirpath ) ) {
44 QString fpath = filepath.mid( dirpath.length() );
45 if ( fpath.startsWith( "/" ) ) fpath.remove( 0, 1 );
47 printf( "2. fpath: %s\n", qPrintable( fpath ) );
55 HXX2Salome::HXX2Salome() : QDialog()
61 HXX2Salome::~HXX2Salome()
66 void HXX2Salome::retrieve()
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 );
97 void HXX2Salome::dump()
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() );
111 void HXX2Salome::on_CloseButton_clicked()
116 void HXX2Salome::on_SourceTreeButton_clicked()
118 QString s = QFileDialog::getExistingDirectory( this,
119 tr( "Get Existing directory" ),
120 SourceTreeText->text() );
121 if ( !s.isEmpty() ) SourceTreeText->setText( s );
124 void HXX2Salome::on_IncludeButton_clicked()
126 QString s = QFileDialog::getOpenFileName( this,
127 tr( "Choose a file to open" ),
129 tr( "Include files (*.h *.hh *.hxx *.hpp)" ) );
130 if ( !s.isEmpty() ) {
131 IncludeText->setText( relFileNameFromDir( SourceTreeText->text().trimmed(), s ) );
135 void HXX2Salome::on_LibraryButton_clicked()
137 QString s = QFileDialog::getOpenFileName( this,
138 tr( "Choose a file to open" ),
140 tr( "Shared Libraries (*.so *.dll)" ) );
141 if ( !s.isEmpty() ) {
142 LibraryText->setText( relFileNameFromDir( SourceTreeText->text().trimmed(), s ) );
146 void HXX2Salome::on_EnvFileButton_clicked()
148 QString s = QFileDialog::getOpenFileName( this,
149 tr( "Choose a script file to open" ),
151 tr( "Environment files (*.csh *.sh)" ) );
152 if ( !s.isEmpty() ) EnvFileText->setText( s );
155 void HXX2Salome::on_OutputTreeButton_clicked()
157 QString s = QFileDialog::getExistingDirectory( this,
158 tr( "Choose a directory" ),
159 OutputTreeText->text() );
160 if ( !s.isEmpty() ) OutputTreeText->setText( s );
163 void HXX2Salome::on_GenerateButton_clicked()
165 // check input validity
166 QString CppDir = SourceTreeText->text().trimmed();
167 QFileInfo fid( CppDir.isEmpty() ? QString( "." ) : CppDir );
169 if ( CppDir.isEmpty() ) {
170 QMessageBox::StandardButton btn =
171 QMessageBox::warning( this,
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;
178 QString CppInc = IncludeText->text().trimmed();
179 if ( CppInc.isEmpty() ) {
180 QMessageBox::critical( this,
182 tr( "Component C++ include file is not specified!" ) );
185 if ( QFileInfo( CppInc ).isAbsolute() ) CppInc = relFileNameFromDir( CppDir, CppInc );
186 if ( QFileInfo( CppInc ).isAbsolute() ) {
187 QMessageBox::critical( this,
189 tr( "Component C++ include file is specified in directory other than\n%1!" ).arg( CppDir ) );
192 CppInc = QFileInfo( CppInc ).fileName();
194 QString CppLib = LibraryText->text().trimmed();
195 if ( CppLib.isEmpty() ) {
196 QMessageBox::critical( this,
198 tr( "Component shared library is not specified!" ) );
201 if ( QFileInfo( CppLib ).isAbsolute() ) CppLib = relFileNameFromDir( CppDir, CppLib );
202 if ( QFileInfo( CppLib ).isAbsolute() ) {
203 QMessageBox::critical( this,
205 tr( "Component shared library is specified in directory other than\n%1!" ).arg( CppDir ) );
208 CppLib = QFileInfo( CppLib ).fileName();
210 QString SalomeDir = OutputTreeText->text().trimmed();
211 QFileInfo fis( SalomeDir.isEmpty() ? QString( "." ) : SalomeDir );
213 if ( SalomeDir.isEmpty() ) {
214 QMessageBox::StandardButton btn =
215 QMessageBox::warning( this,
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;
222 QString EnvFile = EnvFileText->text().trimmed();
223 QFileInfo fienv( EnvFile );
225 // generate command line
227 cmdlist << "${HXX2SALOME_ROOT_DIR}/hxx2salome";
228 if ( MakeGUI->isChecked() )
230 if ( Compile->isChecked() )
232 if ( ShellChoice->currentIndex() == 1 )
234 if ( !EnvFile.isEmpty() ) {
236 cmdlist << quote( fienv.absoluteFilePath() );
238 cmdlist << quote( fid.absoluteFilePath() );
239 cmdlist << quote( CppInc );
240 cmdlist << quote( CppLib );
241 cmdlist << quote( fis.absoluteFilePath() );
242 QString command = cmdlist.join( " " );
246 printf( "command: %s\n", qPrintable( command ) );
248 QApplication::setOverrideCursor( Qt::WaitCursor );
249 std::system( command.toLatin1().constData() );
250 QApplication::restoreOverrideCursor();