Salome HOME
Fix for bug 10438: Crash during Explode on Blocks operation (Global selection on...
[modules/gui.git] / src / Qtx / QtxStdOperations.cxx
index a81c1aad6906ae4a69af84b41cd3686749fd3a91..f6c34bd759057f99aa1dd658d3b2441bc41135e4 100644 (file)
@@ -110,6 +110,7 @@ QtxArithmetics::QtxArithmetics()
     aList.append( "<=" );
     aList.append( ">=" );
     aList.append( "<>" );
+    aList.append( "!=" ); // same as "<>" - for C++ addicts
     addOperations( aList );
 
     ListOfTypes aTypes;
@@ -153,7 +154,7 @@ int QtxArithmetics::prior( const QString& op, bool isBin ) const
 {
     if( isBin )
         if( op=="<" || op==">" || op=="=" || 
-            op=="<=" || op==">=" || op=="<>" )
+            op=="<=" || op==">=" || op=="<>" || op=="!=" )
             return 1;
         else if( op=="+" || op=="-" )
             return 2;
@@ -213,7 +214,7 @@ QtxParser::Error QtxArithmetics::calculate( const QString& op,
                 set( v1, _v1<=_v2 );
             else if( op==">=" )
                 set( v1, _v1>=_v2 );
-            else if( op=="<>" )
+            else if( op=="<>" || op=="!=" )
                 set( v1, _v1!=_v2 );
         }
         else if( ( v1.type()==QVariant::Int || v1.type()==QVariant::Double ) &&
@@ -243,7 +244,7 @@ QtxParser::Error QtxArithmetics::calculate( const QString& op,
                 set( v1, _v1<=_v2 );
             else if( op==">=" )
                 set( v1, _v1>=_v2 );
-            else if( op=="<>" )
+            else if( op=="<>" || op=="!=" )
                 set( v1, _v1!=_v2 );
         }
     else
@@ -288,6 +289,8 @@ QtxLogic::QtxLogic()
 
     ListOfTypes aTypes;
     aTypes.append( QVariant::Bool );
+    aTypes.append( QVariant::Int );
+    aTypes.append( QVariant::UInt );
     addTypes( aTypes );
 }
 
@@ -307,9 +310,9 @@ bool QtxLogic::createValue( const QString& str, QtxValue& v ) const
 {
     bool ok = true;
     if( str.lower()=="true" )
-        v = true;
+        v = QtxValue( true, 0 );
     else if( str.lower()=="false" )
-        v = false;
+        v = QtxValue( false, 0 );
     else
         ok = QtxStdOperations::createValue( str, v );
 
@@ -337,6 +340,18 @@ int QtxLogic::prior( const QString& op, bool isBin ) const
             return 0;
 }
 
+bool boolean_value( const QtxValue& v )
+{
+  if( v.type()==QVariant::Bool )
+    return v.toBool();
+  else if( v.type()==QVariant::Int )
+    return v.toInt()!=0;
+  else if( v.type()==QVariant::UInt )
+    return v.toUInt()!=0;
+  else
+    return false;
+}
+
 //================================================================
 // Function : 
 // Purpose  : 
@@ -345,22 +360,24 @@ QtxParser::Error QtxLogic::calculate( const QString& op,
                                           QtxValue& v1, QtxValue& v2 ) const
 {
     QtxParser::Error err = QtxParser::OK;
+    bool val1 = boolean_value( v1 ),
+         val2 = boolean_value( v2 );
     if( v1.isValid() && v2.isValid() )
     {
         if( op=="and" || op=="&&" )
-            set( v1, v1.toBool() && v2.toBool() );
+            set( v1, val1 && val2 );
         else if( op=="or" || op=="||" )
-            set( v1, v1.toBool() || v2.toBool() );
+            set( v1, val1 || val2 );
         else if( op=="xor" )
-            set( v1, ( !v1.toBool() && v2.toBool() ) || ( v1.toBool() && !v2.toBool() ) );
+            set( v1, ( !val1 && val2 ) || ( val1 && !val2 ) );
         else if( op=="imp" )
-            set( v1, !v1.toBool() || v2.toBool() );
+            set( v1, !val1 || val2 );
         else if( op=="=" )
-            set( v1, v1.toBool()==v2.toBool() );
+            set( v1, val1==val2 );
     }
     else
         if( op=="not" || op=="!" )
-            v2 = !v2.toBool();
+            set( v2, !val2 );
 
     return err;
 }
@@ -486,6 +503,7 @@ QtxStrings::QtxStrings()
     aList.append( "<=" );
     aList.append( ">=" );
     aList.append( "<>" );
+    aList.append( "!=" ); // same as "<>" - for C++ addicts
     aList.append( "length" );
     aList.append( "lower" );
     aList.append( "upper" );
@@ -535,7 +553,7 @@ int QtxStrings::prior( const QString& op, bool isBin ) const
         if( op=="+" ) 
             return 2;
         else if( op=="="  || op=="<"  || op==">"  ||
-                 op=="<=" || op==">=" || op=="<>" )
+                 op=="<=" || op==">=" || op=="<>" || op=="!=" )
             return 1;
         else
             return 0;
@@ -566,7 +584,7 @@ QtxParser::Error QtxStrings::calculate( const QString& op,
             set( v1, _v1<_v2 );
         else if( op==">" )
             set( v1, _v1>_v2 );
-        else if( op=="<>" )
+        else if( op=="<>" || op=="!=" )
             set( v1, _v1!=_v2 );
         else if( op=="<=" )
             set( v1, _v1<_v2 || _v1==_v2 );
@@ -603,6 +621,7 @@ QtxSets::QtxSets()
     aList.append( "}" );
     aList.append( "=" );
     aList.append( "<>" );
+    aList.append( "!=" ); // same as "<>" - for C++ addicts
     aList.append( "+" );
     aList.append( "-" );
     aList.append( "*" );
@@ -652,7 +671,7 @@ bool QtxSets::createValue( const QString& str, QtxValue& val ) const
 int QtxSets::prior( const QString& op, bool isBin ) const
 {
     if( isBin )
-        if( op=="=" || op=="<>" )
+        if( op=="=" || op=="<>" || op=="!=" )
             return 1;
         else if( op=="+" || op=="-" || op=="*" )
             return 2;
@@ -748,19 +767,19 @@ QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue&
             v1 = aNewList;
         }
 
-        else if( op=="=" || op=="<>" || op=="+" || op=="-" || op=="*" )
+        else if( op=="=" || op=="<>" || op=="!=" || op=="+" || op=="-" || op=="*" )
         {
             ValueSet aNewList;
             add( aNewList, v1.toList() );
 
-            if( op=="=" || op=="<>" || op=="-" )
+            if( op=="=" || op=="<>" || op=="!=" || op=="-" )
             {
                 remove( aNewList, v2.toList() );
 
                 if( op=="=" )
-                    set( v1, aNewList.isEmpty() );
-                else if( op=="<>" )
-                    set( v1, !aNewList.isEmpty() );
+                    set( v1, aNewList.isEmpty() && v1.toList().count()==v2.toList().count() );
+                else if( op=="<>" || op=="!=" )
+                    set( v1, !aNewList.isEmpty() || v1.toList().count()!=v2.toList().count() );
                 else
                     v1 = aNewList;
             }
@@ -793,7 +812,10 @@ QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue&
                 set( v1, res );
             }
             else
-                v1 = v2.toList().contains( v1 );
+           {
+             const QValueList< QVariant >& list = v2.toList();
+             v1 = ( bool )( list.find( v1 )!=list.end() );
+           }
         }
 
     return err;