1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "filetemplate.h"
23 #include "guimainwindow.h"
31 //=======================================
32 // Only works if a file is already loaded in engrid (.egc necessary)
33 int fileTemplateTest( int argc
, char ** argv
)
35 QApplication
app( argc
, argv
);
37 QVector
<QString
> files
;
38 files
.push_back( ":/resources/openfoam/simpleFoam/system/fvSchemes.template" );
39 files
.push_back( ":/resources/openfoam/simpleFoam/system/fvSchemes2.template" );
41 TemplateDialog
super_gui( files
, "openfoam/simplefoam/standard/" );
46 //=======================================
47 // Only works if a file is already loaded in engrid (.egc necessary)
48 int fileTemplateTest()
50 QVector
<QString
> files
;
51 files
.push_back( ":/resources/openfoam/simpleFoam/system/fvSchemes.template" );
53 TemplateDialog
super_gui( files
, "openfoam/simplefoam/standard/" );
55 return super_gui
.exec();
57 //=======================================
58 QString
TemplateLine::getDefaultValue()
60 if ( m_DefaultValueEgc
== "" ) return m_DefaultValueOpenFOAM
;
61 else return m_DefaultValueEgc
;
64 void TemplateLine::print()
66 qWarning() << "m_Type=" << this->m_Type
;
67 qWarning() << "m_Name=" << this->m_Name
;
68 qWarning() << "m_Options=" << this->m_Options
;
69 qWarning() << "m_DefaultValueEgc=" << this->m_DefaultValueEgc
;
70 qWarning() << "m_DefaultValueOpenFOAM=" << this->m_DefaultValueOpenFOAM
;
71 qWarning() << "m_Position=" << this->m_Position
;
73 //=======================================
75 FileTemplate::FileTemplate()
79 FileTemplate::FileTemplate( QString filename
, QString section
)
81 this->open( filename
, section
);
84 void FileTemplate::print()
86 for ( int i
= 0; i
< m_Lines
.size(); i
++ ) {
92 int FileTemplate::open( QString filename
, QString section
)
94 m_FileInfo
.setFile( filename
);
95 m_Section
= section
+ "/" + m_FileInfo
.completeBaseName();
96 QFile
file( m_FileInfo
.filePath() );
97 if ( !file
.exists() ) {
98 qWarning() << "ERROR: " << m_FileInfo
.filePath() << " not found.";
100 EG_ERR_RETURN( "ERROR: " + m_FileInfo
.filePath() + " not found." );
102 catch ( Error err
) {
107 if ( !file
.open( QIODevice::ReadOnly
| QIODevice::Text
) ) {
108 qWarning() << "ERROR: Failed to open file " << m_FileInfo
.filePath();
110 EG_ERR_RETURN( "ERROR: Failed to open file " + m_FileInfo
.filePath() );
112 catch ( Error err
) {
117 QTextStream
text_stream( &file
);
118 m_InText
= text_stream
.readAll();
123 this->getValuesFromEgc();
128 int FileTemplate::saveEgc()
130 qWarning() << "Saving EGC ... ";
131 QString contents
= this->getContents();
132 GuiMainWindow::pointer()->setXmlSection( m_Section
, contents
);
136 int FileTemplate::exportToOpenFOAM( QString filename
)
139 this->getValuesFromEgc();
142 m_FileInfo
.setFile( filename
);
143 QFile
file( m_FileInfo
.filePath() );
144 if ( !file
.open( QIODevice::WriteOnly
| QIODevice::Text
) ) {
145 qWarning() << "ERROR: Failed to open file.";
148 QTextStream
out( &file
);
149 m_OutText
= m_InText
;
150 QRegExp
regexp( "{{{.*}}}" );
151 regexp
.setMinimal( true );
152 for ( int i
= 0; i
< m_Lines
.size(); i
++ ) {
153 int idx1
= m_OutText
.indexOf( "{{{" );
154 int idx2
= m_OutText
.indexOf( "}}}" );
155 m_OutText
.replace( idx1
, idx2
- idx1
+ 3, m_Lines
[i
].getDefaultValue() );
162 int FileTemplate::processTemplate()
165 QStringList L_open
= m_InText
.split( "{{{" );
166 for ( int i
= 1; i
< L_open
.size(); i
++ ) {
167 QStringList L_close
= L_open
[i
].split( "}}}" );
168 QStringList L_elements
= L_close
[0].split( ":" );
169 TemplateLine template_line
;
170 template_line
.m_Type
= L_elements
[0];
171 template_line
.m_Name
= L_elements
[1];
172 template_line
.m_Options
= L_elements
[2];
173 template_line
.m_DefaultValueOpenFOAM
= L_elements
[3];
174 template_line
.m_Position
= i
;
175 m_Lines
.push_back( template_line
);
180 QVector
<TemplateLine
> FileTemplate::getLines()
185 void FileTemplate::setLines( QVector
<TemplateLine
> lines
)
190 QString
FileTemplate::getContents()
194 for ( int i
= 0; i
< m_Lines
.size(); i
++ ) {
195 ret
+= m_Lines
[i
].m_Name
+ " = " + m_Lines
[i
].getDefaultValue() + ";\n";
200 void FileTemplate::getValuesFromEgc()
202 QString contents
= GuiMainWindow::pointer()->getXmlSection( m_Section
);
204 QStringList L
= contents
.split( ";" );
205 for ( int i
= 0; i
< L
.size() - 1; i
++ ) {
206 QStringList L_pair
= L
[i
].split( "=" );
207 if ( i
< m_Lines
.size() && L_pair
.size() >= 2 ) {
208 m_Lines
[i
].m_DefaultValueEgc
= L_pair
[1].trimmed();
210 qDebug() << "Warning: Your case file may be incompatible with the current file template.";
214 //=======================================
216 TemplateDialog::TemplateDialog( QVector
<QString
> files
, QString section
, QWidget
*parent
) : QDialog( parent
)
218 this->setWindowTitle( "Template Viewer" );
220 QVBoxLayout
* mainLayout
= new QVBoxLayout( this );
221 this->setLayout( mainLayout
);
223 QPushButton
* openButton
= new QPushButton( "Open...", this );
224 QPushButton
* saveButton
= new QPushButton( "Save", this );
225 QPushButton
* saveAsButton
= new QPushButton( "Save as...", this );
226 connect( openButton
, SIGNAL( clicked() ), this, SLOT( open() ) );
227 connect( saveButton
, SIGNAL( clicked() ), this, SLOT( saveEgc() ) );
228 connect( saveAsButton
, SIGNAL( clicked() ), this, SLOT( saveAs() ) );
230 for ( int i
= 0; i
< files
.size(); i
++ ) {
231 m_FileInfo
.push_back( QFileInfo( files
[i
] ) );
233 TemplateFormLayout
* box
= new TemplateFormLayout( files
, section
);
234 mainLayout
->addLayout( box
);
235 m_TemplateFormLayoutVector
.push_back( box
);
237 QHBoxLayout
*bottomLayout
= new QHBoxLayout
;
238 bottomLayout
->addStretch();
239 bottomLayout
->addWidget( openButton
);
240 bottomLayout
->addWidget( saveButton
);
241 bottomLayout
->addWidget( saveAsButton
);
242 mainLayout
->addLayout( bottomLayout
);
245 void TemplateDialog::saveEgc()
247 for ( int i
= 0; i
< m_TemplateFormLayoutVector
.size(); i
++ ) {
248 m_TemplateFormLayoutVector
[i
]->saveEgc();
252 //=======================================
254 TemplateFormLayout::TemplateFormLayout( QVector
<QString
> filename
, QString section
, char *name
, QWidget
*parent
) : QFormLayout( parent
)
256 GuiMainWindow::pointer();
257 this->setObjectName( name
);
258 this->setLabelAlignment(Qt::AlignLeft
);
259 for ( int filename_index
= 0; filename_index
< filename
.size(); filename_index
++ ) {
260 FileTemplate
file_template( filename
[filename_index
], section
);
262 QVector
<TemplateLine
> lines
= file_template
.getLines();
263 for ( int i
= 0; i
< lines
.size(); i
++ ) {
264 if ( lines
[i
].m_Type
== "ComboBox" ) addComboBox( lines
[i
] );
265 else if ( lines
[i
].m_Type
== "IntLineEdit" ) addIntLineEdit( lines
[i
] );
266 else if ( lines
[i
].m_Type
== "DoubleLineEdit" ) addDoubleLineEdit( lines
[i
] );
267 else if ( lines
[i
].m_Type
== "TextLineEdit" ) addTextLineEdit( lines
[i
] );
268 else if ( lines
[i
].m_Type
== "CheckBox" ) addCheckBox( lines
[i
] );
269 else if ( lines
[i
].m_Type
== "SpinBox" ) addSpinBox( lines
[i
] );
270 else if ( lines
[i
].m_Type
== "DoubleSpinBox" ) addDoubleSpinBox( lines
[i
] );
271 else qWarning() << "Unknown type";
273 m_FileTemplate
.push_back( file_template
);
277 void TemplateFormLayout::addComboBox( TemplateLine line
)
279 QComboBox
* combobox
= new QComboBox
;
280 QStringList description
;
282 QStringList L_open
= line
.m_Options
.split( "(" );
283 for ( int i
= 1; i
< L_open
.size(); i
++ ) {
284 QStringList L_close
= L_open
[i
].split( ")" );
285 QStringList L_elements
= L_close
[0].split( "," );
286 description
<< L_elements
[0];
287 value
<< L_elements
[1].trimmed();
289 int current
= value
.indexOf( line
.getDefaultValue().trimmed() );
290 combobox
->addItems( description
);
291 combobox
-> setCurrentIndex( current
);
292 this->addRow( line
.m_Name
, combobox
);
293 m_ComboBoxVector
.push_back( combobox
);
294 m_ComboboxValues
.push_back( value
);
297 void TemplateFormLayout::addIntLineEdit( TemplateLine line
)
299 QValidator
*validator
= new QIntValidator( this );
300 QLineEdit
* int_lineedit
= new QLineEdit
;
301 int_lineedit
->setValidator( validator
);
302 int_lineedit
->setText( line
.getDefaultValue().trimmed() );
303 this->addRow( line
.m_Name
, int_lineedit
);
304 m_IntLineEditVector
.push_back( int_lineedit
);
307 void TemplateFormLayout::addDoubleLineEdit( TemplateLine line
)
309 QValidator
*validator
= new QDoubleValidator( this );
310 QLineEdit
* double_lineedit
= new QLineEdit
;
311 double_lineedit
->setValidator( validator
);
312 double_lineedit
->setText( line
.getDefaultValue().trimmed() );
313 this->addRow( line
.m_Name
, double_lineedit
);
314 m_DoubleLineEditVector
.push_back( double_lineedit
);
317 void TemplateFormLayout::addTextLineEdit( TemplateLine line
)
319 QLineEdit
* text_lineedit
= new QLineEdit
;
320 text_lineedit
->setText( line
.getDefaultValue().trimmed() );
321 this->addRow( line
.m_Name
, text_lineedit
);
322 m_TextLineEditVector
.push_back( text_lineedit
);
325 void TemplateFormLayout::addCheckBox( TemplateLine line
)
327 QCheckBox
* check_box
= new QCheckBox
;
328 QStringList L
= line
.m_Options
.split( "," );
329 L
[0] = L
[0].trimmed();
330 L
[1] = L
[1].trimmed();
331 int index
= L
.indexOf( line
.getDefaultValue().trimmed() );
332 if ( index
== 0 ) check_box
->setCheckState( Qt::Checked
);
333 else check_box
->setCheckState( Qt::Unchecked
);
334 QPair
< QString
, QString
> values
;
336 values
.second
= L
[1];
337 this->addRow( line
.m_Name
, check_box
);
338 m_CheckBoxVector
.push_back( check_box
);
339 m_CheckBoxValues
.push_back( values
);
342 void TemplateFormLayout::addSpinBox( TemplateLine line
)
344 QSpinBox
* spin_box
= new QSpinBox
;
345 QStringList L
= line
.m_Options
.split( "," );
346 int minimum
= L
[0].trimmed().toInt();
347 int maximum
= L
[1].trimmed().toInt();
348 int step
= L
[2].trimmed().toInt();
349 int value
= line
.getDefaultValue().trimmed().toInt();
350 spin_box
->setRange( minimum
, maximum
);
351 spin_box
->setSingleStep( step
);
352 spin_box
->setValue( value
);
353 this->addRow( line
.m_Name
, spin_box
);
354 m_SpinBoxVector
.push_back( spin_box
);
357 void TemplateFormLayout::addDoubleSpinBox( TemplateLine line
)
359 QDoubleSpinBox
* double_spin_box
= new QDoubleSpinBox
;
360 QStringList L
= line
.m_Options
.split( "," );
361 double minimum
= L
[0].trimmed().toDouble();
362 double maximum
= L
[1].trimmed().toDouble();
363 double step
= L
[2].trimmed().toDouble();
364 int decimals
= L
[3].trimmed().toInt();
365 double value
= line
.getDefaultValue().trimmed().toDouble();
366 double_spin_box
->setRange( minimum
, maximum
);
367 double_spin_box
->setSingleStep( step
);
368 double_spin_box
->setDecimals( decimals
);
369 double_spin_box
->setValue( value
);
370 this->addRow( line
.m_Name
, double_spin_box
);
371 m_DoubleSpinBoxVector
.push_back( double_spin_box
);
372 // this->itemAt(this->rowCount()-1)->setAlignment(Qt::AlignRight);
375 void TemplateFormLayout::saveEgc()
377 int combobox_idx
= 0;
378 int intlineedit_idx
= 0;
379 int doublelineedit_idx
= 0;
380 int textlineedit_idx
= 0;
381 int checkbox_idx
= 0;
383 int doublespinbox_idx
= 0;
384 for ( int file_template_index
= 0; file_template_index
< m_FileTemplate
.size(); file_template_index
++ ) {
385 QVector
<TemplateLine
> lines
= m_FileTemplate
[file_template_index
].getLines();
386 for ( int i
= 0; i
< lines
.size(); i
++ ) {
387 if ( lines
[i
].m_Type
== "ComboBox" ) {
388 lines
[i
].m_DefaultValueEgc
= readComboBox( combobox_idx
);
391 else if ( lines
[i
].m_Type
== "IntLineEdit" ) {
392 lines
[i
].m_DefaultValueEgc
= readIntLineEdit( intlineedit_idx
);
395 else if ( lines
[i
].m_Type
== "DoubleLineEdit" ) {
396 lines
[i
].m_DefaultValueEgc
= readDoubleLineEdit( doublelineedit_idx
);
397 doublelineedit_idx
++;
399 else if ( lines
[i
].m_Type
== "TextLineEdit" ) {
400 lines
[i
].m_DefaultValueEgc
= readTextLineEdit( textlineedit_idx
);
403 else if ( lines
[i
].m_Type
== "CheckBox" ) {
404 lines
[i
].m_DefaultValueEgc
= readCheckBox( checkbox_idx
);
407 else if ( lines
[i
].m_Type
== "SpinBox" ) {
408 lines
[i
].m_DefaultValueEgc
= readSpinBox( spinbox_idx
);
411 else if ( lines
[i
].m_Type
== "DoubleSpinBox" ) {
412 lines
[i
].m_DefaultValueEgc
= readDoubleSpinBox( doublespinbox_idx
);
415 else qWarning() << "Unknown type";
417 m_FileTemplate
[file_template_index
].setLines( lines
);
418 m_FileTemplate
[file_template_index
].saveEgc();
422 QString
TemplateFormLayout::readComboBox( int idx
)
424 int i
= m_ComboBoxVector
[idx
]->currentIndex();
425 if( i
== -1 ) {// security in case nothing has been selected
427 qDebug()<<"WARNING: Nothing has been selected in combobox "<<idx
;
429 return m_ComboboxValues
[idx
][i
];
432 QString
TemplateFormLayout::readIntLineEdit( int idx
)
434 return m_IntLineEditVector
[idx
]->text();
437 QString
TemplateFormLayout::readDoubleLineEdit( int idx
)
439 return m_DoubleLineEditVector
[idx
]->text();
442 QString
TemplateFormLayout::readTextLineEdit( int idx
)
444 return m_TextLineEditVector
[idx
]->text();
447 QString
TemplateFormLayout::readCheckBox( int idx
)
449 if ( m_CheckBoxVector
[idx
]->checkState() == Qt::Checked
) return m_CheckBoxValues
[idx
].first
;
450 else return m_CheckBoxValues
[idx
].second
;
453 QString
TemplateFormLayout::readSpinBox( int idx
)
456 ret
.setNum( m_SpinBoxVector
[idx
]->value() );
460 QString
TemplateFormLayout::readDoubleSpinBox( int idx
)
463 ret
.setNum( m_DoubleSpinBoxVector
[idx
]->value() );
466 //=======================================