2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2013 enGits GmbH +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "filetemplate.h"
25 #include "guimainwindow.h"
33 //=======================================
34 // Only works if a file is already loaded in engrid (.egc necessary)
35 int fileTemplateTest( int argc
, char ** argv
)
37 QApplication
app( argc
, argv
);
39 QVector
<QString
> files
;
40 files
.push_back( ":/resources/openfoam/simpleFoam/system/fvSchemes.template" );
41 files
.push_back( ":/resources/openfoam/simpleFoam/system/fvSchemes2.template" );
43 TemplateDialog
super_gui( files
, "openfoam/simplefoam/standard/" );
48 //=======================================
49 // Only works if a file is already loaded in engrid (.egc necessary)
50 int fileTemplateTest()
52 QVector
<QString
> files
;
53 files
.push_back( ":/resources/openfoam/simpleFoam/system/fvSchemes.template" );
55 TemplateDialog
super_gui( files
, "openfoam/simplefoam/standard/" );
57 return super_gui
.exec();
59 //=======================================
60 QString
TemplateLine::getDefaultValue()
62 if ( m_DefaultValueEgc
== "" ) return m_DefaultValueOpenFOAM
;
63 else return m_DefaultValueEgc
;
66 void TemplateLine::print()
68 qWarning() << "m_Type=" << this->m_Type
;
69 qWarning() << "m_Name=" << this->m_Name
;
70 qWarning() << "m_Options=" << this->m_Options
;
71 qWarning() << "m_DefaultValueEgc=" << this->m_DefaultValueEgc
;
72 qWarning() << "m_DefaultValueOpenFOAM=" << this->m_DefaultValueOpenFOAM
;
73 qWarning() << "m_Position=" << this->m_Position
;
75 //=======================================
77 FileTemplate::FileTemplate()
81 FileTemplate::FileTemplate( QString filename
, QString section
)
83 this->open( filename
, section
);
86 void FileTemplate::print()
88 for ( int i
= 0; i
< m_Lines
.size(); i
++ ) {
94 int FileTemplate::open( QString filename
, QString section
)
96 m_FileInfo
.setFile( filename
);
97 m_Section
= section
+ "/" + m_FileInfo
.completeBaseName();
98 QFile
file( m_FileInfo
.filePath() );
99 if ( !file
.exists() ) {
100 qWarning() << "ERROR: " << m_FileInfo
.filePath() << " not found.";
102 EG_ERR_RETURN( "ERROR: " + m_FileInfo
.filePath() + " not found." );
104 catch ( Error err
) {
109 if ( !file
.open( QIODevice::ReadOnly
| QIODevice::Text
) ) {
110 qWarning() << "ERROR: Failed to open file " << m_FileInfo
.filePath();
112 EG_ERR_RETURN( "ERROR: Failed to open file " + m_FileInfo
.filePath() );
114 catch ( Error err
) {
119 QTextStream
text_stream( &file
);
120 m_InText
= text_stream
.readAll();
125 this->getValuesFromEgc();
130 int FileTemplate::saveEgc()
132 qWarning() << "Saving EGC ... ";
133 QString contents
= this->getContents();
134 GuiMainWindow::pointer()->setXmlSection( m_Section
, contents
);
138 int FileTemplate::exportToOpenFOAM( QString filename
)
141 this->getValuesFromEgc();
144 m_FileInfo
.setFile( filename
);
145 QFile
file( m_FileInfo
.filePath() );
146 if ( !file
.open( QIODevice::WriteOnly
| QIODevice::Text
) ) {
147 qWarning() << "ERROR: Failed to open file.";
150 QTextStream
out( &file
);
151 m_OutText
= m_InText
;
152 QRegExp
regexp( "{{{.*}}}" );
153 regexp
.setMinimal( true );
154 for ( int i
= 0; i
< m_Lines
.size(); i
++ ) {
155 int idx1
= m_OutText
.indexOf( "{{{" );
156 int idx2
= m_OutText
.indexOf( "}}}" );
157 m_OutText
.replace( idx1
, idx2
- idx1
+ 3, m_Lines
[i
].getDefaultValue() );
164 int FileTemplate::processTemplate()
167 QStringList L_open
= m_InText
.split( "{{{" );
168 for ( int i
= 1; i
< L_open
.size(); i
++ ) {
169 QStringList L_close
= L_open
[i
].split( "}}}" );
170 QStringList L_elements
= L_close
[0].split( ":" );
171 TemplateLine template_line
;
172 template_line
.m_Type
= L_elements
[0];
173 template_line
.m_Name
= L_elements
[1];
174 template_line
.m_Options
= L_elements
[2];
175 template_line
.m_DefaultValueOpenFOAM
= L_elements
[3];
176 template_line
.m_Position
= i
;
177 m_Lines
.push_back( template_line
);
182 QVector
<TemplateLine
> FileTemplate::getLines()
187 void FileTemplate::setLines( QVector
<TemplateLine
> lines
)
192 QString
FileTemplate::getContents()
196 for ( int i
= 0; i
< m_Lines
.size(); i
++ ) {
197 ret
+= m_Lines
[i
].m_Name
+ " = " + m_Lines
[i
].getDefaultValue() + ";\n";
202 void FileTemplate::getValuesFromEgc()
204 QString contents
= GuiMainWindow::pointer()->getXmlSection( m_Section
);
206 QStringList L
= contents
.split( ";" );
207 for ( int i
= 0; i
< L
.size() - 1; i
++ ) {
208 QStringList L_pair
= L
[i
].split( "=" );
209 if ( i
< m_Lines
.size() && L_pair
.size() >= 2 ) {
210 m_Lines
[i
].m_DefaultValueEgc
= L_pair
[1].trimmed();
212 qDebug() << "Warning: Your case file may be incompatible with the current file template.";
216 //=======================================
218 TemplateDialog::TemplateDialog( QVector
<QString
> files
, QString section
, QWidget
*parent
) : QDialog( parent
)
220 this->setWindowTitle( "Template Viewer" );
222 QVBoxLayout
* mainLayout
= new QVBoxLayout( this );
223 this->setLayout( mainLayout
);
225 QPushButton
* openButton
= new QPushButton( "Open...", this );
226 QPushButton
* saveButton
= new QPushButton( "Save", this );
227 QPushButton
* saveAsButton
= new QPushButton( "Save as...", this );
228 connect( openButton
, SIGNAL( clicked() ), this, SLOT( open() ) );
229 connect( saveButton
, SIGNAL( clicked() ), this, SLOT( saveEgc() ) );
230 connect( saveAsButton
, SIGNAL( clicked() ), this, SLOT( saveAs() ) );
232 for ( int i
= 0; i
< files
.size(); i
++ ) {
233 m_FileInfo
.push_back( QFileInfo( files
[i
] ) );
235 TemplateFormLayout
* box
= new TemplateFormLayout( files
, section
);
236 mainLayout
->addLayout( box
);
237 m_TemplateFormLayoutVector
.push_back( box
);
239 QHBoxLayout
*bottomLayout
= new QHBoxLayout
;
240 bottomLayout
->addStretch();
241 bottomLayout
->addWidget( openButton
);
242 bottomLayout
->addWidget( saveButton
);
243 bottomLayout
->addWidget( saveAsButton
);
244 mainLayout
->addLayout( bottomLayout
);
247 void TemplateDialog::saveEgc()
249 for ( int i
= 0; i
< m_TemplateFormLayoutVector
.size(); i
++ ) {
250 m_TemplateFormLayoutVector
[i
]->saveEgc();
254 //=======================================
256 TemplateFormLayout::TemplateFormLayout( QVector
<QString
> filename
, QString section
, char *name
, QWidget
*parent
) : QFormLayout( parent
)
258 GuiMainWindow::pointer();
259 this->setObjectName( name
);
260 this->setLabelAlignment(Qt::AlignLeft
);
261 for ( int filename_index
= 0; filename_index
< filename
.size(); filename_index
++ ) {
262 FileTemplate
file_template( filename
[filename_index
], section
);
264 QVector
<TemplateLine
> lines
= file_template
.getLines();
265 for ( int i
= 0; i
< lines
.size(); i
++ ) {
266 if ( lines
[i
].m_Type
== "ComboBox" ) addComboBox( lines
[i
] );
267 else if ( lines
[i
].m_Type
== "IntLineEdit" ) addIntLineEdit( lines
[i
] );
268 else if ( lines
[i
].m_Type
== "DoubleLineEdit" ) addDoubleLineEdit( lines
[i
] );
269 else if ( lines
[i
].m_Type
== "TextLineEdit" ) addTextLineEdit( lines
[i
] );
270 else if ( lines
[i
].m_Type
== "CheckBox" ) addCheckBox( lines
[i
] );
271 else if ( lines
[i
].m_Type
== "SpinBox" ) addSpinBox( lines
[i
] );
272 else if ( lines
[i
].m_Type
== "DoubleSpinBox" ) addDoubleSpinBox( lines
[i
] );
273 else qWarning() << "Unknown type";
275 m_FileTemplate
.push_back( file_template
);
279 void TemplateFormLayout::addComboBox( TemplateLine line
)
281 QComboBox
* combobox
= new QComboBox
;
282 QStringList description
;
284 QStringList L_open
= line
.m_Options
.split( "(" );
285 for ( int i
= 1; i
< L_open
.size(); i
++ ) {
286 QStringList L_close
= L_open
[i
].split( ")" );
287 QStringList L_elements
= L_close
[0].split( "," );
288 description
<< L_elements
[0];
289 value
<< L_elements
[1].trimmed();
291 int current
= value
.indexOf( line
.getDefaultValue().trimmed() );
292 combobox
->addItems( description
);
293 combobox
-> setCurrentIndex( current
);
294 this->addRow( line
.m_Name
, combobox
);
295 m_ComboBoxVector
.push_back( combobox
);
296 m_ComboboxValues
.push_back( value
);
299 void TemplateFormLayout::addIntLineEdit( TemplateLine line
)
301 QValidator
*validator
= new QIntValidator( this );
302 QLineEdit
* int_lineedit
= new QLineEdit
;
303 int_lineedit
->setValidator( validator
);
304 int_lineedit
->setText( line
.getDefaultValue().trimmed() );
305 this->addRow( line
.m_Name
, int_lineedit
);
306 m_IntLineEditVector
.push_back( int_lineedit
);
309 void TemplateFormLayout::addDoubleLineEdit( TemplateLine line
)
311 QValidator
*validator
= new QDoubleValidator( this );
312 QLineEdit
* double_lineedit
= new QLineEdit
;
313 double_lineedit
->setValidator( validator
);
314 double_lineedit
->setText( line
.getDefaultValue().trimmed() );
315 this->addRow( line
.m_Name
, double_lineedit
);
316 m_DoubleLineEditVector
.push_back( double_lineedit
);
319 void TemplateFormLayout::addTextLineEdit( TemplateLine line
)
321 QLineEdit
* text_lineedit
= new QLineEdit
;
322 text_lineedit
->setText( line
.getDefaultValue().trimmed() );
323 this->addRow( line
.m_Name
, text_lineedit
);
324 m_TextLineEditVector
.push_back( text_lineedit
);
327 void TemplateFormLayout::addCheckBox( TemplateLine line
)
329 QCheckBox
* check_box
= new QCheckBox
;
330 QStringList L
= line
.m_Options
.split( "," );
331 L
[0] = L
[0].trimmed();
332 L
[1] = L
[1].trimmed();
333 int index
= L
.indexOf( line
.getDefaultValue().trimmed() );
334 if ( index
== 0 ) check_box
->setCheckState( Qt::Checked
);
335 else check_box
->setCheckState( Qt::Unchecked
);
336 QPair
< QString
, QString
> values
;
338 values
.second
= L
[1];
339 this->addRow( line
.m_Name
, check_box
);
340 m_CheckBoxVector
.push_back( check_box
);
341 m_CheckBoxValues
.push_back( values
);
344 void TemplateFormLayout::addSpinBox( TemplateLine line
)
346 QSpinBox
* spin_box
= new QSpinBox
;
347 QStringList L
= line
.m_Options
.split( "," );
348 int minimum
= L
[0].trimmed().toInt();
349 int maximum
= L
[1].trimmed().toInt();
350 int step
= L
[2].trimmed().toInt();
351 int value
= line
.getDefaultValue().trimmed().toInt();
352 spin_box
->setRange( minimum
, maximum
);
353 spin_box
->setSingleStep( step
);
354 spin_box
->setValue( value
);
355 this->addRow( line
.m_Name
, spin_box
);
356 m_SpinBoxVector
.push_back( spin_box
);
359 void TemplateFormLayout::addDoubleSpinBox( TemplateLine line
)
361 QDoubleSpinBox
* double_spin_box
= new QDoubleSpinBox
;
362 QStringList L
= line
.m_Options
.split( "," );
363 double minimum
= L
[0].trimmed().toDouble();
364 double maximum
= L
[1].trimmed().toDouble();
365 double step
= L
[2].trimmed().toDouble();
366 int decimals
= L
[3].trimmed().toInt();
367 double value
= line
.getDefaultValue().trimmed().toDouble();
368 double_spin_box
->setRange( minimum
, maximum
);
369 double_spin_box
->setSingleStep( step
);
370 double_spin_box
->setDecimals( decimals
);
371 double_spin_box
->setValue( value
);
372 this->addRow( line
.m_Name
, double_spin_box
);
373 m_DoubleSpinBoxVector
.push_back( double_spin_box
);
374 // this->itemAt(this->rowCount()-1)->setAlignment(Qt::AlignRight);
377 void TemplateFormLayout::saveEgc()
379 int combobox_idx
= 0;
380 int intlineedit_idx
= 0;
381 int doublelineedit_idx
= 0;
382 int textlineedit_idx
= 0;
383 int checkbox_idx
= 0;
385 int doublespinbox_idx
= 0;
386 for ( int file_template_index
= 0; file_template_index
< m_FileTemplate
.size(); file_template_index
++ ) {
387 QVector
<TemplateLine
> lines
= m_FileTemplate
[file_template_index
].getLines();
388 for ( int i
= 0; i
< lines
.size(); i
++ ) {
389 if ( lines
[i
].m_Type
== "ComboBox" ) {
390 lines
[i
].m_DefaultValueEgc
= readComboBox( combobox_idx
);
393 else if ( lines
[i
].m_Type
== "IntLineEdit" ) {
394 lines
[i
].m_DefaultValueEgc
= readIntLineEdit( intlineedit_idx
);
397 else if ( lines
[i
].m_Type
== "DoubleLineEdit" ) {
398 lines
[i
].m_DefaultValueEgc
= readDoubleLineEdit( doublelineedit_idx
);
399 doublelineedit_idx
++;
401 else if ( lines
[i
].m_Type
== "TextLineEdit" ) {
402 lines
[i
].m_DefaultValueEgc
= readTextLineEdit( textlineedit_idx
);
405 else if ( lines
[i
].m_Type
== "CheckBox" ) {
406 lines
[i
].m_DefaultValueEgc
= readCheckBox( checkbox_idx
);
409 else if ( lines
[i
].m_Type
== "SpinBox" ) {
410 lines
[i
].m_DefaultValueEgc
= readSpinBox( spinbox_idx
);
413 else if ( lines
[i
].m_Type
== "DoubleSpinBox" ) {
414 lines
[i
].m_DefaultValueEgc
= readDoubleSpinBox( doublespinbox_idx
);
417 else qWarning() << "Unknown type";
419 m_FileTemplate
[file_template_index
].setLines( lines
);
420 m_FileTemplate
[file_template_index
].saveEgc();
424 QString
TemplateFormLayout::readComboBox( int idx
)
426 int i
= m_ComboBoxVector
[idx
]->currentIndex();
427 if( i
== -1 ) {// security in case nothing has been selected
429 qDebug()<<"WARNING: Nothing has been selected in combobox "<<idx
;
431 return m_ComboboxValues
[idx
][i
];
434 QString
TemplateFormLayout::readIntLineEdit( int idx
)
436 return m_IntLineEditVector
[idx
]->text();
439 QString
TemplateFormLayout::readDoubleLineEdit( int idx
)
441 return m_DoubleLineEditVector
[idx
]->text();
444 QString
TemplateFormLayout::readTextLineEdit( int idx
)
446 return m_TextLineEditVector
[idx
]->text();
449 QString
TemplateFormLayout::readCheckBox( int idx
)
451 if ( m_CheckBoxVector
[idx
]->checkState() == Qt::Checked
) return m_CheckBoxValues
[idx
].first
;
452 else return m_CheckBoxValues
[idx
].second
;
455 QString
TemplateFormLayout::readSpinBox( int idx
)
458 ret
.setNum( m_SpinBoxVector
[idx
]->value() );
462 QString
TemplateFormLayout::readDoubleSpinBox( int idx
)
465 ret
.setNum( m_DoubleSpinBoxVector
[idx
]->value() );
468 //=======================================