Salome HOME
Lot4: boundary conditions dialog (finalization).
authormzn <mzn@opencascade.com>
Tue, 18 Oct 2016 11:44:10 +0000 (14:44 +0300)
committermzn <mzn@opencascade.com>
Tue, 18 Oct 2016 11:44:10 +0000 (14:44 +0300)
src/HYDROGUI/BndConditionsDialog.py
src/HYDROGUI/BndConditionsDialog.ui

index e4d7968e1a33348eb15da1bcf64d7d28540e38bb..2d1200e68f539973277027e68fd08079f30bddaa 100644 (file)
@@ -20,13 +20,9 @@ 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
+from MEDLoader import MEDFileMesh
 
-# TODO: get rid of the nex one line
+# TODO: get rid of sys.path.append() ?
 sys.path.append(os.path.join(os.environ['HYDROSOLVER_ROOT_DIR'], 'src', 'HYDROTools'))
 import boundaryConditions
 
@@ -34,6 +30,76 @@ from Ui_BndConditionsDialog import Ui_BoundaryConditionsDialog
 
 ROW_PROPERTY_NAME = "row"
 
+"""Get names of groups on edges from the given MED file"""
+def get_med_groups_on_edges(file_path):
+    try:
+        med_file_mesh = MEDFileMesh.New(file_path)
+        groups = list(med_file_mesh.getGroupsOnSpecifiedLev(-1))
+    except:
+        return []
+          
+    return groups
+
+"""Get preset name corresponding to the given values of LIHBOR, LIUBOR, LIVBOR and LITBOR"""    
+def get_preset_name(presets, lihbor, liubor, livbor, litbor):
+    name = ''
+    
+    for preset_name in presets:
+        values = presets[preset_name]
+        
+        p_lihbor = values[0]
+        p_liubor = values[1]
+        p_livbor = values[2]
+        p_litbor = values[3]
+        
+        if not p_lihbor or p_lihbor == lihbor:
+            if not p_liubor or p_liubor == liubor:
+                if not p_livbor or p_livbor == livbor:
+                    if not p_litbor or p_litbor == litbor:
+                        name = preset_name
+                        
+    return name
+
+"""Convert string to integer, return None if conversion is not possible"""    
+def get_int(str):
+    value = None
+    
+    if str.isdigit():
+        value = int(str)
+            
+    return value
+
+"""Item delegate for LIHBOR, LIUBOR, LIVBOR and LITBOR columns"""    
+class ValueDelegate(QtGui.QStyledItemDelegate):
+    
+    def __init__(self, parent = None):
+        QtGui.QStyledItemDelegate.__init__(self, parent)
+        
+    def createEditor(self, parent, option, index):
+        line_edit = QtGui.QLineEdit(parent)
+        
+        validator = QtGui.QIntValidator(parent)
+        validator.setRange(0, 6)
+        
+        line_edit.setValidator(validator)
+
+        return line_edit
+        
+    def setEditorData(self, editor, index):
+        value, is_ok = index.model().data(index, QtCore.Qt.EditRole).toInt()
+
+        if is_ok:
+            editor.setText(str(value))
+        else:
+            editor.setText('')
+    
+    def setModelData(self, editor, model, index):
+        model.setData(index, editor.text(), QtCore.Qt.EditRole)
+
+    def updateEditorGeometry(self, editor, option, index):
+        editor.setGeometry(option.rect)
+    
+"""Boundary conditions definition dialog"""    
 class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
 
     def __init__(self, parent = None, modal = 0):
@@ -49,9 +115,12 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
         self.sameAsInputCB.toggled.connect(self.resultBndConditionsFileEdit.setDisabled)
         self.sameAsInputCB.toggled.connect(self.resultBndConditionsFileButton.setDisabled)
         
+        self.boundaryConditionsTable.cellChanged.connect(self.on_cell_changed)
+        
         self.applyAndCloseButton.clicked.connect(self.on_apply_and_close)
         self.applyButton.clicked.connect(self.on_apply)
         self.closeButton.clicked.connect(self.reject)
+        self.helpButton.clicked.connect(self.on_help)
         
         # Set widgets properties
         self.init_widgets()
@@ -65,7 +134,7 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
     
     """Initialize presets"""
     def init_presets(self):
-        # TODO: fix file path
+        # TODO: determine another presets 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()
@@ -76,7 +145,39 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
         self.bndConditionsFileEdit.setReadOnly(True)
         self.resultBndConditionsFileEdit.setReadOnly(True)
         
-        #TODO: provide browse icon for tool buttons
+        delegate = ValueDelegate(self.boundaryConditionsTable)
+        self.boundaryConditionsTable.setItemDelegateForColumn(1, delegate)
+        self.boundaryConditionsTable.setItemDelegateForColumn(2, delegate)
+        self.boundaryConditionsTable.setItemDelegateForColumn(3, delegate)
+        self.boundaryConditionsTable.setItemDelegateForColumn(4, delegate)
+    
+    """Process cell data changes"""
+    def on_cell_changed(self, row, column):
+        lihbor = liubor = livbor = litbor = None
+        
+        item = self.boundaryConditionsTable.item(row, 1)
+        if item:
+            lihbor = get_int(str(item.text()))
+        
+        item = self.boundaryConditionsTable.item(row, 2)
+        if item:
+            liubor = get_int(str(item.text()))
+            
+        item = self.boundaryConditionsTable.item(row, 3)
+        if item:
+            livbor = get_int(str(item.text()))
+            
+        item = self.boundaryConditionsTable.item(row, 4)
+        if item:
+            litbor = get_int(str(item.text()))
+            
+        preset_name = get_preset_name(self.presets, lihbor, liubor, livbor, litbor)
+        
+        combo = self.boundaryConditionsTable.cellWidget(row, 0)
+        if isinstance(combo, QtGui.QComboBox):
+            ind = combo.findText(preset_name)
+            if ind >= 0:
+                combo.setCurrentIndex(ind)
     
     """Save the user data to boundary conditions file"""
     def on_apply(self):
@@ -104,28 +205,30 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
     def on_apply_and_close(self):
         if self.on_apply():
             self.accept()
+            
+    """Shows help page"""
+    def on_help(self):
+        # TODO: to be implemented
+        pass
     
     """Select MED file"""
     def on_med_file_browse(self):
+        # Get file path
         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"]
+        # Get names of groups on edges
+        groups = get_med_groups_on_edges(str(file_path))
         
         if len(groups) > 0:
+            # Display the path
+            self.medFileEdit.setText(file_path)
+            
+            # Update table
             self.set_groups(groups)
         else:
-            QtGui.QMessageBox.warning(self, self.tr("Warning"), self.tr("The selected MED file does not contain groups on edges."))
+            QtGui.QMessageBox.warning(self, self.tr("Warning"), self.tr("Can't get group names from the selected MED file."))
     
     """Select boundary conditions file"""
     def on_bnd_file_browse(self):
@@ -138,7 +241,7 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
 
             self.input_conditions.clear()
             for cnd in conditions:
-                self.input_conditions[cnd.group] = (cnd.lihbor, cnd.livbor, cnd.liubor, cnd.litbor)
+                self.input_conditions[cnd.group] = (cnd.lihbor, cnd.liubor, cnd.livbor, cnd.litbor)
                             
             if len(self.input_conditions) > 0:
                 self.update_table()
@@ -212,13 +315,21 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
             self.boundaryConditionsTable.setItem(row_nb, 4, QtGui.QTableWidgetItem(''))
             
             # 'Group' column
-            self.boundaryConditionsTable.setItem(row_nb, 5, QtGui.QTableWidgetItem(group))
+            item = QtGui.QTableWidgetItem(group)
+            font = item.font()
+            font.setBold(True)
+            item.setFont(font)
+            item.setFlags(QtCore.Qt.ItemIsEnabled)
+            self.boundaryConditionsTable.setItem(row_nb, 5, item)
         
         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()):
+        is_updated = False
+    
+        nb_rows = self.boundaryConditionsTable.rowCount()   
+        for row_nb in xrange(0, nb_rows):
             group_name = str(self.boundaryConditionsTable.item(row_nb, 5).text())
             if self.input_conditions.has_key(group_name):
                 values = self.input_conditions[group_name]
@@ -232,6 +343,11 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
                 self.boundaryConditionsTable.item(row_nb, 2).setText(liubor)
                 self.boundaryConditionsTable.item(row_nb, 3).setText(livbor)
                 self.boundaryConditionsTable.item(row_nb, 4).setText(litbor)
+                
+                is_updated = True
+                
+        if not is_updated and nb_rows > 0 and len(self.input_conditions) > 0:
+            QtGui.QMessageBox.information(self, self.tr("Information"), self.tr("No one group name from the MED file is presented in the input list of conditions."))
     
     """Get output file path"""
     def get_output_path(self):
@@ -246,13 +362,25 @@ class BoundaryConditionsDialog(Ui_BoundaryConditionsDialog, QtGui.QDialog):
     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
+            has_empty_cells = False
+            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())
+                
+                if (not lihbor) or (not liubor) or (not livbor) or (not litbor):
+                    has_empty_cells = True
+                    break
+            
+            if has_empty_cells:
+                QtGui.QMessageBox.critical(self, self.tr("Insufficient input data"), self.tr("Table has empty cell(s)."))
+            else:
+                is_ok = True
             
         return is_ok
\ No newline at end of file
index 18b5889054edea5edda45245b00e978438659e18..1660156139928cd4dfbb3355a5dac8704de3e3dc 100644 (file)
       </item>
       <item row="0" column="2">
        <widget class="QToolButton" name="medFileButton">
-        <property name="text">
-         <string/>
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
         </property>
-        <property name="icon">
-         <iconset>
-          <normaloff>../../../../../SALOME-7.6.0/MODULES/HYDRO/HYDRO_SRC/src/HYDROGUI/resources/icon_browse.png</normaloff>../../../../../SALOME-7.6.0/MODULES/HYDRO/HYDRO_SRC/src/HYDROGUI/resources/icon_browse.png</iconset>
+        <property name="text">
+         <string>...</string>
         </property>
        </widget>
       </item>
       </item>
       <item row="1" column="2">
        <widget class="QToolButton" name="bndConditionsFileButton">
-        <property name="text">
-         <string/>
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
         </property>
-        <property name="icon">
-         <iconset>
-          <normaloff>../../../../../SALOME-7.6.0/MODULES/HYDRO/HYDRO_SRC/src/HYDROGUI/resources/icon_browse.png</normaloff>../../../../../SALOME-7.6.0/MODULES/HYDRO/HYDRO_SRC/src/HYDROGUI/resources/icon_browse.png</iconset>
+        <property name="text">
+         <string>...</string>
         </property>
        </widget>
       </item>
         </item>
         <item>
          <widget class="QToolButton" name="resultBndConditionsFileButton">
-          <property name="text">
-           <string/>
+          <property name="sizePolicy">
+           <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+            <horstretch>0</horstretch>
+            <verstretch>0</verstretch>
+           </sizepolicy>
           </property>
-          <property name="icon">
-           <iconset>
-            <normaloff>../../../../../SALOME-7.6.0/MODULES/HYDRO/HYDRO_SRC/src/HYDROGUI/resources/icon_browse.png</normaloff>../../../../../SALOME-7.6.0/MODULES/HYDRO/HYDRO_SRC/src/HYDROGUI/resources/icon_browse.png</iconset>
+          <property name="text">
+           <string>...</string>
           </property>
          </widget>
         </item>
        </property>
       </widget>
      </item>
+     <item>
+      <widget class="QPushButton" name="closeButton">
+       <property name="text">
+        <string>Close</string>
+       </property>
+      </widget>
+     </item>
      <item>
       <spacer name="horizontalSpacer">
        <property name="orientation">
       </spacer>
      </item>
      <item>
-      <widget class="QPushButton" name="closeButton">
+      <widget class="QPushButton" name="helpButton">
        <property name="text">
-        <string>Close</string>
+        <string>Help</string>
        </property>
       </widget>
      </item>