Salome HOME
refs #497: redesign of HYDRO main menu.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PriorityTableModel.cxx
index 16023dad5bfd8c71d1d23d4facc3daab4089583b..fb5b8c6da5cbd071f4405f4e7e03e94728149001 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -230,21 +230,22 @@ QVariant HYDROGUI_PriorityTableModel::headerData( int theSection,
 }
 
 /**
-  Set objects which could be used for rules.
-  @param theObjects the list of objects
+  Set objects which could be used for rules definition.
+  @param theObjects the ordered list of objects
  */
 void HYDROGUI_PriorityTableModel::setObjects( const QList<Handle(HYDROData_Object)>& theObjects )
 {
   myObjects = theObjects;
-
+    
   beginResetModel();
 
   // Remove rules which use objects which are no longer available
+  QStringList aNames = getAvailableObjectNames();
   QMutableListIterator<HYDROData_CustomRule> anIter( myRules );
   while ( anIter.hasNext() ) {
     HYDROData_CustomRule aRule = anIter.next();
-     if ( !myObjects.contains( aRule.Object1 ) || 
-          !myObjects.contains( aRule.Object2 ) ) {
+     if ( !aNames.contains( aRule.Object1->GetName() ) || 
+          !aNames.contains( aRule.Object2->GetName() ) ) {
       anIter.remove();
     }
   }
@@ -261,20 +262,24 @@ bool HYDROGUI_PriorityTableModel::createNewRule()
   if ( !canCreateNewRule() ) {
     return false;
   }
-
+  
   // Existing pairs of object indexes
+  QStringList aNames = getAvailableObjectNames();
   QList< QPair<int, int> > aRules;
   foreach( const HYDROData_CustomRule& aRule, getRules() ) {
-    int anIndex1 = myObjects.indexOf( aRule.Object1 );
-    int anIndex2 = myObjects.indexOf( aRule.Object2 );
+    int anIndex1 = aNames.indexOf( aRule.Object1->GetName() );
+    int anIndex2 = aNames.indexOf( aRule.Object2->GetName() );
     if ( anIndex1 >= 0 && anIndex2 >= 0 ) {
       aRules << QPair<int, int>( anIndex1, anIndex2 );
       aRules << QPair<int, int>( anIndex2, anIndex1 );
     }
   }
     
-  // Try to find new pair of objects
+  // Try to find:
+  // 1. the new pair of objects 
+  // 2. the priority type corresponding to the objects order
   Handle(HYDROData_Object) anObject1, anObject2;
+  HYDROData_PriorityType aPriorityType = GREATER;
 
   int aNbObjects = myObjects.count();
   for ( int anIndex1 = 0; anIndex1 < aNbObjects; anIndex1++ ) {
@@ -288,6 +293,9 @@ bool HYDROGUI_PriorityTableModel::createNewRule()
       if ( !aRules.contains( QPair<int, int>( anIndex1, anIndex2 ) ) ) {
         anObject1 = myObjects.at( anIndex1 );
         anObject2 = myObjects.at( anIndex2 );
+        if ( anIndex1 > anIndex2 ) {
+          aPriorityType = LESS;
+        }
         isFound = true;
         break;
       }
@@ -305,7 +313,7 @@ bool HYDROGUI_PriorityTableModel::createNewRule()
     HYDROData_CustomRule aNewRule;
     aNewRule.Object1 = anObject1;
     aNewRule.Object2 = anObject2;
-    aNewRule.Priority = LESS;
+    aNewRule.Priority = aPriorityType;
     aNewRule.MergeType = HYDROData_Zone::Merge_ZMIN;
 
     beginResetModel();
@@ -372,10 +380,9 @@ QStringList HYDROGUI_PriorityTableModel::getAvailablePairs(  const Handle(HYDROD
 {
   QStringList aNames;
 
-  foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
-    if ( !anObj.IsNull() && anObj->GetName() != theObject->GetName() ) {
-      aNames << anObj->GetName();
-    }
+  if ( !theObject.IsNull() ) {
+    aNames = getAvailableObjectNames();
+    aNames.removeAll( theObject->GetName() );
   }
 
   return aNames;
@@ -459,4 +466,21 @@ bool HYDROGUI_PriorityTableModel::isUsed( const Handle(HYDROData_Object)& theObj
   }       
 
   return isUsed;
+}
+
+/**
+  Get available object names.
+  @return the list of object names
+ */
+QStringList HYDROGUI_PriorityTableModel::getAvailableObjectNames() const
+{
+  QStringList aNames;
+
+  foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
+    if ( !anObj.IsNull() ) {
+      aNames << anObj->GetName();
+    }
+  }
+
+  return aNames;
 }
\ No newline at end of file