2 #include <QtCore/QFile>
3 #include <QtCore/QFileInfo>
4 #include <QtCore/QTextStream>
5 #include <QtGui/QApplication>
6 #include <QtGui/QFileDialog>
7 #include <QtGui/QMessageBox>
9 #include "hxx2salome.h"
11 // VSR: uncomment for debug mode
14 static QString quote( const QString& val )
17 if ( !v.startsWith( "\"" ) ) v.prepend( "\"" );
18 if ( !v.endsWith( "\"" ) ) v.append( "\"" );
22 static QString unquote( const QString& val )
25 if ( v.startsWith( "\"" ) ) v = v.remove( 0, 1 );
26 if ( v.endsWith( "\"" ) ) v = v.remove( v.length()-1, 1 );
30 static QString relFileNameFromDir( const QString& dir, const QString& filename )
33 printf( "0. dir: %s, filename: %s\n", qPrintable( dir ), qPrintable( filename ) );
35 if ( !filename.isEmpty() ) {
36 QString dirpath = QFileInfo( dir.isEmpty() ? QDir::currentPath() : dir ).absoluteFilePath();
37 QString filepath = QFileInfo( filename ).absoluteFilePath();
39 printf( "1. dirpath: %s, filepath: %s\n", qPrintable( dirpath ), qPrintable( filepath ) );
41 if ( filepath.startsWith( dirpath ) ) {
42 QString fpath = filepath.mid( dirpath.length() );
43 if ( fpath.startsWith( "/" ) ) fpath.remove( 0, 1 );
45 printf( "2. fpath: %s\n", qPrintable( fpath ) );
53 HXX2Salome::HXX2Salome() : QDialog()
59 HXX2Salome::~HXX2Salome()
64 void HXX2Salome::retrieve()
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 );
95 void HXX2Salome::dump()
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() );
109 void HXX2Salome::on_CloseButton_clicked()
114 void HXX2Salome::on_SourceTreeButton_clicked()
116 QString s = QFileDialog::getExistingDirectory( this,
117 tr( "Get Existing directory" ),
118 SourceTreeText->text() );
119 if ( !s.isEmpty() ) SourceTreeText->setText( s );
122 void HXX2Salome::on_IncludeButton_clicked()
124 QString s = QFileDialog::getOpenFileName( this,
125 tr( "Choose a file to open" ),
127 tr( "Include files (*.h *.hh *.hxx *.hpp)" ) );
128 if ( !s.isEmpty() ) {
129 IncludeText->setText( relFileNameFromDir( SourceTreeText->text().trimmed(), s ) );
133 void HXX2Salome::on_LibraryButton_clicked()
135 QString s = QFileDialog::getOpenFileName( this,
136 tr( "Choose a file to open" ),
138 tr( "Shared Libraries (*.so *.dll)" ) );
139 if ( !s.isEmpty() ) {
140 LibraryText->setText( relFileNameFromDir( SourceTreeText->text().trimmed(), s ) );
144 void HXX2Salome::on_EnvFileButton_clicked()
146 QString s = QFileDialog::getOpenFileName( this,
147 tr( "Choose a script file to open" ),
149 tr( "Environment files (*.csh *.sh)" ) );
150 if ( !s.isEmpty() ) EnvFileText->setText( s );
153 void HXX2Salome::on_OutputTreeButton_clicked()
155 QString s = QFileDialog::getExistingDirectory( this,
156 tr( "Choose a directory" ),
157 OutputTreeText->text() );
158 if ( !s.isEmpty() ) OutputTreeText->setText( s );
161 void HXX2Salome::on_GenerateButton_clicked()
163 // check input validity
164 QString CppDir = SourceTreeText->text().trimmed();
165 QFileInfo fid( CppDir.isEmpty() ? QString( "." ) : CppDir );
167 if ( CppDir.isEmpty() ) {
168 QMessageBox::StandardButton btn =
169 QMessageBox::warning( this,
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;
176 QString CppInc = IncludeText->text().trimmed();
177 if ( CppInc.isEmpty() ) {
178 QMessageBox::critical( this,
180 tr( "Component C++ include file is not specified!" ) );
183 if ( QFileInfo( CppInc ).isAbsolute() ) CppInc = relFileNameFromDir( CppDir, CppInc );
184 if ( QFileInfo( CppInc ).isAbsolute() ) {
185 QMessageBox::critical( this,
187 tr( "Component C++ include file is specified in directory other than\n%1!" ).arg( CppDir ) );
190 CppInc = QFileInfo( CppInc ).fileName();
192 QString CppLib = LibraryText->text().trimmed();
193 if ( CppLib.isEmpty() ) {
194 QMessageBox::critical( this,
196 tr( "Component shared library is not specified!" ) );
199 if ( QFileInfo( CppLib ).isAbsolute() ) CppLib = relFileNameFromDir( CppDir, CppLib );
200 if ( QFileInfo( CppLib ).isAbsolute() ) {
201 QMessageBox::critical( this,
203 tr( "Component shared library is specified in directory other than\n%1!" ).arg( CppDir ) );
206 CppLib = QFileInfo( CppLib ).fileName();
208 QString SalomeDir = OutputTreeText->text().trimmed();
209 QFileInfo fis( SalomeDir.isEmpty() ? QString( "." ) : SalomeDir );
211 if ( SalomeDir.isEmpty() ) {
212 QMessageBox::StandardButton btn =
213 QMessageBox::warning( this,
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;
220 QString EnvFile = EnvFileText->text().trimmed();
221 QFileInfo fienv( EnvFile );
223 // generate command line
225 cmdlist << "${HXX2SALOME_ROOT_DIR}/hxx2salome";
226 if ( MakeGUI->isChecked() )
228 if ( Compile->isChecked() )
230 if ( ShellChoice->currentIndex() == 1 )
232 if ( !EnvFile.isEmpty() ) {
234 cmdlist << quote( fienv.absoluteFilePath() );
236 cmdlist << quote( fid.absoluteFilePath() );
237 cmdlist << quote( CppInc );
238 cmdlist << quote( CppLib );
239 cmdlist << quote( fis.absoluteFilePath() );
240 QString command = cmdlist.join( " " );
244 printf( "command: %s\n", qPrintable( command ) );
246 QApplication::setOverrideCursor( Qt::WaitCursor );
247 std::system( command.toLatin1().constData() );
248 QApplication::restoreOverrideCursor();