1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "QtxStdOperations.h"
24 //================================================================
27 //================================================================
28 QtxStdOperations::QtxStdOperations()
32 //================================================================
35 //================================================================
36 QtxStdOperations::~QtxStdOperations()
40 //================================================================
43 //================================================================
44 void QtxStdOperations::bracketsList( QStringList& list, bool open ) const
52 //================================================================
55 //================================================================
56 void QtxStdOperations::opersList( QStringList& list ) const
61 //================================================================
64 //================================================================
65 void QtxStdOperations::addOperations( const QStringList& list )
67 QStringList::const_iterator anIt = list.begin(),
69 for( ; anIt!=aLast; anIt++ )
70 if( myOpers.contains( *anIt )==0 )
71 myOpers.append( *anIt );
74 //================================================================
77 //================================================================
78 void QtxStdOperations::addTypes( const ListOfTypes& list )
80 ListOfTypes::const_iterator anIt = list.begin(),
82 for( ; anIt!=aLast; anIt++ )
83 if( myTypes.contains( *anIt )==0 )
84 myTypes.append( *anIt );
87 //================================================================
90 //================================================================
91 QtxParser::Error QtxStdOperations::isValid( const QString& op,
92 const QVariant::Type t1,
93 const QVariant::Type t2 ) const
95 if( ( t1==QVariant::Invalid || myTypes.contains( t1 ) ) &&
96 ( t2==QVariant::Invalid || myTypes.contains( t2 ) ) &&
97 ( t1!=QVariant::Invalid || t2!=QVariant::Invalid ) )
98 if( prior( op, t1!=QVariant::Invalid && t2!=QVariant::Invalid ) > 0 )
101 return QtxParser::InvalidOperation;
103 return QtxParser::OperandsNotMatch;
113 //================================================================
116 //================================================================
117 QtxArithmetics::QtxArithmetics()
128 aList.append( "<=" );
129 aList.append( ">=" );
130 aList.append( "<>" );
131 aList.append( "!=" ); // same as "<>" - for C++ addicts
132 addOperations( aList );
135 aTypes.append( QVariant::Int );
136 aTypes.append( QVariant::UInt );
137 aTypes.append( QVariant::Double );
141 //================================================================
144 //================================================================
145 QtxArithmetics::~QtxArithmetics()
149 //================================================================
152 //================================================================
153 bool QtxArithmetics::createValue( const QString& str, QtxValue& v ) const
156 v = str.toInt( &ok );
160 v = str.toDouble( &ok );
162 ok = QtxStdOperations::createValue( str, v );
167 //================================================================
170 //================================================================
171 int QtxArithmetics::prior( const QString& op, bool isBin ) const
174 if( op=="<" || op==">" || op=="=" ||
175 op=="<=" || op==">=" || op=="<>" || op=="!=" )
177 else if( op=="+" || op=="-" )
179 else if( op=="*" || op=="/" )
183 else if( op=="+" || op=="-" )
189 void set( QVariant& v1, bool v2 )
191 v1 = QVariant( v2, 0 );
194 //================================================================
197 //================================================================
198 QtxParser::Error QtxArithmetics::calculate( const QString& op,
199 QtxValue& v1, QtxValue& v2 ) const
201 QtxParser::Error err = QtxParser::OK;
203 if( v1.isValid() && v2.isValid() )
205 if( ( v1.type()==QVariant::Int || v1.type()==QVariant::UInt ) &&
206 ( v2.type()==QVariant::Int || v2.type()==QVariant::UInt ) )
208 int _v1 = v1.toInt(),
222 v1 = double( _v1 ) / double( _v2 );
224 err = QtxParser::InvalidResult;
235 else if( op=="<>" || op=="!=" )
238 else if( ( v1.type()==QVariant::Int || v1.type()==QVariant::Double ) &&
239 ( v2.type()==QVariant::Int || v2.type()==QVariant::Double ) )
241 double _v1 = v1.toDouble(),
254 err = QtxParser::InvalidResult;
265 else if( op=="<>" || op=="!=" )
271 if( v2.type()==QVariant::Int )
273 else if( v2.type()==QVariant::Double )
289 //================================================================
292 //================================================================
297 aList.append( "and" );
298 aList.append( "&&" );
299 aList.append( "or" );
300 aList.append( "||" );
301 aList.append( "xor" );
302 aList.append( "not" );
304 aList.append( "imp" );
306 addOperations( aList );
309 aTypes.append( QVariant::Bool );
310 aTypes.append( QVariant::Int );
311 aTypes.append( QVariant::UInt );
315 //================================================================
318 //================================================================
319 QtxLogic::~QtxLogic()
323 //================================================================
326 //================================================================
327 bool QtxLogic::createValue( const QString& str, QtxValue& v ) const
330 if( str.lower()=="true" )
331 v = QtxValue( true, 0 );
332 else if( str.lower()=="false" )
333 v = QtxValue( false, 0 );
335 ok = QtxStdOperations::createValue( str, v );
340 //================================================================
343 //================================================================
344 int QtxLogic::prior( const QString& op, bool isBin ) const
347 if( op=="and" || op=="or" || op=="xor" ||
348 op=="&&" || op=="||" ||
355 else if( op=="not" || op=="!" )
361 bool boolean_value( const QtxValue& v )
363 if( v.type()==QVariant::Bool )
365 else if( v.type()==QVariant::Int )
367 else if( v.type()==QVariant::UInt )
368 return v.toUInt()!=0;
373 //================================================================
376 //================================================================
377 QtxParser::Error QtxLogic::calculate( const QString& op,
378 QtxValue& v1, QtxValue& v2 ) const
380 QtxParser::Error err = QtxParser::OK;
381 bool val1 = boolean_value( v1 ),
382 val2 = boolean_value( v2 );
383 if( v1.isValid() && v2.isValid() )
385 if( op=="and" || op=="&&" )
386 set( v1, val1 && val2 );
387 else if( op=="or" || op=="||" )
388 set( v1, val1 || val2 );
390 set( v1, ( !val1 && val2 ) || ( val1 && !val2 ) );
392 set( v1, !val1 || val2 );
394 set( v1, val1==val2 );
397 if( op=="not" || op=="!" )
408 //================================================================
411 //================================================================
412 QtxFunctions::QtxFunctions()
416 aList.append( "sqrt" );
417 aList.append( "abs" );
418 aList.append( "sin" );
419 aList.append( "cos" );
420 aList.append( "rad2grad" );
421 aList.append( "grad2rad" );
422 addOperations( aList );
425 aTypes.append( QVariant::Int );
426 aTypes.append( QVariant::Double );
430 //================================================================
433 //================================================================
434 QtxFunctions::~QtxFunctions()
438 //================================================================
441 //================================================================
442 bool QtxFunctions::createValue( const QString& str, QtxValue& v ) const
445 v = str.toInt( &ok );
449 v = str.toDouble( &ok );
451 ok = QtxStdOperations::createValue( str, v );
456 //================================================================
459 //================================================================
460 int QtxFunctions::prior( const QString& op, bool isBin ) const
464 else if( op=="sqrt" || op=="abs" || op=="sin" || op=="cos" ||
465 op=="rad2grad" || op=="grad2rad" )
471 //================================================================
474 //================================================================
475 QtxParser::Error QtxFunctions::calculate( const QString& op,
476 QtxValue& v1, QtxValue& v2 ) const
478 QtxParser::Error err = QtxParser::OK;
479 double val = v2.toDouble();
484 err = QtxParser::InvalidResult;
486 if( v2.type()==QVariant::Int )
487 v2 = abs( v2.toInt() );
489 v2 = fabs( v2.toDouble() );
494 else if( op=="grad2rad" )
495 v2 = val * 3.14159256 / 180.0;
496 else if( op=="rad2grad" )
497 v2 = val * 180.0 / 3.14159256;
509 //================================================================
512 //================================================================
513 QtxStrings::QtxStrings()
521 aList.append( "<=" );
522 aList.append( ">=" );
523 aList.append( "<>" );
524 aList.append( "!=" ); // same as "<>" - for C++ addicts
525 aList.append( "length" );
526 aList.append( "lower" );
527 aList.append( "upper" );
528 addOperations( aList );
531 aTypes.append( QVariant::Int );
532 aTypes.append( QVariant::Double );
533 aTypes.append( QVariant::String );
534 aTypes.append( QVariant::CString );
539 //================================================================
542 //================================================================
543 QtxStrings::~QtxStrings()
547 //================================================================
550 //================================================================
551 bool QtxStrings::createValue( const QString& str, QtxValue& v ) const
554 fin = str[ ( int )( str.length()-1 ) ];
555 if( st=="'" && fin=="'" )
557 v = str.mid( 1, str.length()-2 );
561 return QtxStdOperations::createValue( str, v );
564 //================================================================
567 //================================================================
568 int QtxStrings::prior( const QString& op, bool isBin ) const
573 else if( op=="=" || op=="<" || op==">" ||
574 op=="<=" || op==">=" || op=="<>" || op=="!=" )
579 if( op=="length" || op=="lower" || op=="upper" )
585 //================================================================
588 //================================================================
589 QtxParser::Error QtxStrings::calculate( const QString& op,
590 QtxValue& v1, QtxValue& v2 ) const
592 QtxParser::Error err = QtxParser::OK;
593 if( v1.isValid() && v2.isValid() )
595 QString _v1 = v1.toString(),
605 else if( op=="<>" || op=="!=" )
608 set( v1, _v1<_v2 || _v1==_v2 );
610 set( v1, _v1>_v2 || _v1==_v2 );
612 else if( !v1.isValid() && v2.isValid() )
614 QString val = v2.toString();
616 v2 = ( int )val.length();
617 else if( op=="lower" )
619 else if( op=="upper" )
631 //================================================================
634 //================================================================
641 aList.append( "<>" );
642 aList.append( "!=" ); // same as "<>" - for C++ addicts
646 aList.append( "in" );
647 aList.append( "count" );
648 addOperations( aList );
651 aTypes.append( QVariant::List );
655 //================================================================
658 //================================================================
663 //================================================================
666 //================================================================
667 void QtxSets::bracketsList( QStringList& list, bool open ) const
673 QtxStdOperations::bracketsList( list, open );
676 //================================================================
679 //================================================================
680 bool QtxSets::createValue( const QString& str, QtxValue& val ) const
682 return QtxStdOperations::createValue( str, val );
685 //================================================================
688 //================================================================
689 int QtxSets::prior( const QString& op, bool isBin ) const
692 if( op=="=" || op=="<>" || op=="!=" )
694 else if( op=="+" || op=="-" || op=="*" )
702 if( op=="{" || op=="}" )
704 else if( op=="count" )
710 //================================================================
713 //================================================================
714 QtxParser::Error QtxSets::isValid( const QString& op,
715 const QVariant::Type t1,
716 const QVariant::Type t2 ) const
719 return QtxParser::OK;
721 if( t1!=QVariant::Invalid && t2==QVariant::List )
722 return QtxParser::OK;
724 return QtxParser::OperandsNotMatch;
726 return QtxStdOperations::isValid( op, t1, t2 );
729 //================================================================
732 //================================================================
733 void QtxSets::add( ValueSet& set, const QtxValue& v )
735 if( v.isValid() && set.contains( v )==0 )
739 //================================================================
742 //================================================================
743 void QtxSets::add( ValueSet& s1, const ValueSet& s2 )
745 ValueSet::const_iterator anIt = s2.begin(),
747 for( ; anIt!=aLast; anIt++ )
751 //================================================================
754 //================================================================
755 void QtxSets::remove( ValueSet& set, const QtxValue& v )
760 //================================================================
763 //================================================================
764 void QtxSets::remove( ValueSet& s1, const ValueSet& s2 )
766 ValueSet::const_iterator anIt = s2.begin(),
768 for( ; anIt!=aLast; anIt++ )
772 //================================================================
775 //================================================================
776 QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue& v2 ) const
778 QtxParser::Error err = QtxParser::OK;
784 add( aNewList, v1.toList() );
788 else if( op=="=" || op=="<>" || op=="!=" || op=="+" || op=="-" || op=="*" )
791 add( aNewList, v1.toList() );
793 if( op=="=" || op=="<>" || op=="!=" || op=="-" )
795 remove( aNewList, v2.toList() );
798 set( v1, aNewList.isEmpty() && v1.toList().count()==v2.toList().count() );
799 else if( op=="<>" || op=="!=" )
800 set( v1, !aNewList.isEmpty() || v1.toList().count()!=v2.toList().count() );
806 add( aNewList, v2.toList() );
812 add( toDelete, aNewList );
813 remove( toDelete, v2.toList() );
814 remove( aNewList, toDelete );
818 else if( op=="count" )
819 v2 = ( int )v2.toList().count();
822 if( v1.type()==QVariant::List )
825 ValueSet::const_iterator anIt = v1.toList().begin(),
826 aLast = v1.toList().end();
827 for( ; anIt!=aLast && res; anIt++ )
828 res = v2.toList().contains( *anIt )>0;
834 const QValueList< QVariant >& list = v2.toList();
835 v1 = ( bool )( list.find( v1 )!=list.end() );