--- /dev/null
+# Copyright (C) 2012-2013 EDF
+#
+# This file is part of SALOME HYDRO module.
+#
+# SALOME HYDRO module is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# SALOME HYDRO module is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with SALOME HYDRO module. If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import sys
+
+from PyQt4 import QtCore, QtGui
+
+# TODO: replace the two lines below with one: 'from med import medfile'
+sys.path.append(os.path.join(os.environ['MEDFILE_ROOT_DIR'], 'lib\python2.7\site-packages\med'))
+import medfile
+import medmesh
+import medenum
+
+# TODO: get rid of the nex one line
+sys.path.append(os.path.join(os.environ['HYDROSOLVER_ROOT_DIR'], 'src', 'HYDROTools'))
+import boundaryConditions
+
+from Ui_BndConditionsDialog import Ui_BoundaryConditionsDialog
+
+ROW_PROPERTY_NAME = "row"
+
+class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
+
+ def __init__(self, parent = None, modal = 0):
+ QtGui.QDialog.__init__(self, parent)
+ Ui_BoundaryConditionsDialog.__init__(self)
+ self.setupUi(self)
+
+ # Connections
+ self.medFileButton.clicked.connect(self.on_med_file_browse)
+ self.bndConditionsFileButton.clicked.connect(self.on_bnd_file_browse)
+ self.resultBndConditionsFileButton.clicked.connect(self.on_result_file_browse)
+
+ self.sameAsInputCB.toggled.connect(self.resultBndConditionsFileEdit.setDisabled)
+ self.sameAsInputCB.toggled.connect(self.resultBndConditionsFileButton.setDisabled)
+
+ self.applyAndCloseButton.clicked.connect(self.on_apply_and_close)
+ self.applyButton.clicked.connect(self.on_apply)
+ self.closeButton.clicked.connect(self.reject)
+
+ # Set widgets properties
+ self.init_widgets()
+
+ # Input conditions
+ self.input_conditions = {}
+
+ # Init presets
+ self.presets = {}
+ self.init_presets()
+
+ """Initialize presets"""
+ def init_presets(self):
+ # TODO: fix file path
+ file_path = os.path.join(os.environ['HYDROSOLVER_ROOT_DIR'], 'tests', 'data', 'bnd_conditions_presets.txt')
+ reader = boundaryConditions.PresetReader(file_path)
+ self.presets = reader.read()
+
+ """Initialize widget properties"""
+ def init_widgets(self):
+ self.medFileEdit.setReadOnly(True)
+ self.bndConditionsFileEdit.setReadOnly(True)
+ self.resultBndConditionsFileEdit.setReadOnly(True)
+
+ #TODO: provide browse icon for tool buttons
+
+ """Save the user data to boundary conditions file"""
+ def on_apply(self):
+ # Save boundary conditions file
+ if not self.is_valid():
+ return False
+
+ file_path = self.resultBndConditionsFileEdit.text()
+ writer = boundaryConditions.BoundaryConditionWriter(file_path)
+
+ conditions = []
+ for row_nb in xrange(0, self.boundaryConditionsTable.rowCount()):
+ lihbor = str(self.boundaryConditionsTable.item(row_nb, 1).text())
+ liubor = str(self.boundaryConditionsTable.item(row_nb, 2).text())
+ livbor = str(self.boundaryConditionsTable.item(row_nb, 3).text())
+ litbor = str(self.boundaryConditionsTable.item(row_nb, 4).text())
+ group_name = str(self.boundaryConditionsTable.item(row_nb, 5).text())
+ conditions.append(boundaryConditions.BoundaryCondition(lihbor, liubor, livbor, litbor, group_name))
+
+ writer.write(conditions)
+
+ return True
+
+ """Save the user data to boundary conditions file and close the dialog"""
+ def on_apply_and_close(self):
+ if self.on_apply():
+ self.accept()
+
+ """Select MED file"""
+ def on_med_file_browse(self):
+ file_path = QtGui.QFileDialog.getOpenFileName(self, self.tr("Open MED file"), "", self.tr("MED files (*.med)"))
+ if not file_path:
+ return
+
+ # Display the path
+ self.medFileEdit.setText(file_path)
+
+ # Open MED file
+ fid = medfile.MEDfileOpen(str(file_path), medenum.MED_ACC_RDEXT)
+ mesh_name, sdim, mdim, meshtype, desc, dtunit, sort, nstep, repere, axisname, axisunit = medmesh.MEDmeshInfo(fid, 1)
+
+ # Get group names
+ #TODO: get group names from the MED file vis MED Python API
+ groups = ["litMineur_aval", "litMineur_amont", "group3", "group4"]
+
+ if len(groups) > 0:
+ self.set_groups(groups)
+ else:
+ QtGui.QMessageBox.warning(self, self.tr("Warning"), self.tr("The selected MED file does not contain groups on edges."))
+
+ """Select boundary conditions file"""
+ def on_bnd_file_browse(self):
+ file_path = QtGui.QFileDialog.getOpenFileName(self, self.tr("Open boundary conditions file"))
+
+ if file_path:
+ self.bndConditionsFileEdit.setText(file_path)
+ reader = boundaryConditions.BoundaryConditionReader(file_path)
+ conditions = reader.read()
+
+ self.input_conditions.clear()
+ for cnd in conditions:
+ self.input_conditions[cnd.group] = (cnd.lihbor, cnd.livbor, cnd.liubor, cnd.litbor)
+
+ if len(self.input_conditions) > 0:
+ self.update_table()
+ else:
+ QtGui.QMessageBox.warning(self, self.tr("Warning"), self.tr("No conditions have been read from the file."))
+
+ """Called on preset selection in the first column of the table"""
+ def on_preset_changed(self):
+ combo = self.sender()
+
+ preset = str(combo.currentText())
+
+ if preset and self.presets.has_key(preset):
+ values = self.presets[preset]
+ row_nb, is_ok = combo.property(ROW_PROPERTY_NAME).toInt()
+
+ if is_ok and row_nb >= 0 and row_nb < self.boundaryConditionsTable.rowCount():
+ lihbor = values[0]
+ liubor = values[1]
+ livbor = values[2]
+ litbor = values[3]
+
+ if lihbor:
+ self.boundaryConditionsTable.item(row_nb, 1).setText(str(lihbor))
+
+ if liubor:
+ self.boundaryConditionsTable.item(row_nb, 2).setText(str(liubor))
+
+ if livbor:
+ self.boundaryConditionsTable.item(row_nb, 3).setText(str(livbor))
+
+ if litbor:
+ self.boundaryConditionsTable.item(row_nb, 4).setText(str(litbor))
+
+ """Define result file path"""
+ def on_result_file_browse(self):
+ file_path = QtGui.QFileDialog.getSaveFileName(self, self.tr("Select output file path"))
+ if file_path:
+ self.resultBndConditionsFileEdit.setText(file_path)
+
+ """Set groups list"""
+ def set_groups(self, groups):
+ self.boundaryConditionsTable.setRowCount(0)
+ for group in groups:
+ # Add row
+ row_nb = self.boundaryConditionsTable.rowCount()
+ self.boundaryConditionsTable.insertRow(row_nb)
+
+ # 'Preset' column
+ combo = QtGui.QComboBox(self)
+ combo.addItem('')
+ if len(self.presets) > 0:
+ combo.addItems(self.presets.keys())
+
+ combo.setProperty(ROW_PROPERTY_NAME, row_nb)
+
+ combo.currentIndexChanged.connect(self.on_preset_changed)
+
+ self.boundaryConditionsTable.setCellWidget(row_nb, 0, combo)
+
+ # 'LIHBOR' column
+ self.boundaryConditionsTable.setItem(row_nb, 1, QtGui.QTableWidgetItem(''))
+
+ # 'LIUBOR' column
+ self.boundaryConditionsTable.setItem(row_nb, 2, QtGui.QTableWidgetItem(''))
+
+ # 'LIVBOR' column
+ self.boundaryConditionsTable.setItem(row_nb, 3, QtGui.QTableWidgetItem(''))
+
+ # 'LITBOR' column
+ self.boundaryConditionsTable.setItem(row_nb, 4, QtGui.QTableWidgetItem(''))
+
+ # 'Group' column
+ self.boundaryConditionsTable.setItem(row_nb, 5, QtGui.QTableWidgetItem(group))
+
+ self.update_table()
+
+ """Update conditions data in the table from the conditions input file"""
+ def update_table(self):
+ for row_nb in xrange(0, self.boundaryConditionsTable.rowCount()):
+ group_name = str(self.boundaryConditionsTable.item(row_nb, 5).text())
+ if self.input_conditions.has_key(group_name):
+ values = self.input_conditions[group_name]
+
+ lihbor = str(values[0])
+ liubor = str(values[1])
+ livbor = str(values[2])
+ litbor = str(values[3])
+
+ self.boundaryConditionsTable.item(row_nb, 1).setText(lihbor)
+ self.boundaryConditionsTable.item(row_nb, 2).setText(liubor)
+ self.boundaryConditionsTable.item(row_nb, 3).setText(livbor)
+ self.boundaryConditionsTable.item(row_nb, 4).setText(litbor)
+
+ """Get output file path"""
+ def get_output_path(self):
+ path = self.bndConditionsFileEdit.text()
+
+ if not self.sameAsInputCB.isChecked():
+ path = self.resultBndConditionsFileEdit.text()
+
+ return path
+
+ """Check if the input data is valid"""
+ def is_valid(self):
+ is_ok = False
+
+ # TODO: check for empty cells in the table
+
+ if self.boundaryConditionsTable.rowCount() < 1:
+ QtGui.QMessageBox.critical(self, self.tr("Insufficient input data"), self.tr("Boundary conditions list is empty."))
+ elif self.get_output_path().isEmpty():
+ QtGui.QMessageBox.critical(self, self.tr("Insufficient input data"), self.tr("Output file path is empty."))
+ else:
+ is_ok = True
+
+ return is_ok
\ No newline at end of file