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"
27 QtxStdOperations::QtxStdOperations()
34 QtxStdOperations::~QtxStdOperations()
39 Fills list of brackets treated as open (when 'open' is 'true') or close ('open' is 'false')
41 void QtxStdOperations::bracketsList( QStringList& list, bool open ) const
50 Fills list with operation names by copying of internal list of operations
52 void QtxStdOperations::opersList( QStringList& list ) const
58 Add operation names from list to internal list of operations
60 void QtxStdOperations::addOperations( const QStringList& list )
62 QStringList::const_iterator anIt = list.begin(),
64 for( ; anIt!=aLast; anIt++ )
65 if( myOpers.contains( *anIt )==0 )
66 myOpers.append( *anIt );
70 Append operation names from 'list' to internal list of operations
72 void QtxStdOperations::addTypes( const ListOfTypes& list )
74 ListOfTypes::const_iterator anIt = list.begin(),
76 for( ; anIt!=aLast; anIt++ )
77 if( myTypes.contains( *anIt )==0 )
78 myTypes.append( *anIt );
82 \return whether values with passed types are valid for arguments of operation
83 \param op - name of operation
84 \param t1 - type of first argument
85 \param t2 - type of second argument
87 QtxParser::Error QtxStdOperations::isValid( const QString& op,
88 const QVariant::Type t1,
89 const QVariant::Type t2 ) const
91 if( ( t1==QVariant::Invalid || myTypes.contains( t1 ) ) &&
92 ( t2==QVariant::Invalid || myTypes.contains( t2 ) ) &&
93 ( t1!=QVariant::Invalid || t2!=QVariant::Invalid ) )
94 if( prior( op, t1!=QVariant::Invalid && t2!=QVariant::Invalid ) > 0 )
97 return QtxParser::InvalidOperation;
99 return QtxParser::OperandsNotMatch;
112 QtxArithmetics::QtxArithmetics()
123 aList.append( "<=" );
124 aList.append( ">=" );
125 aList.append( "<>" );
126 aList.append( "!=" ); // same as "<>" - for C++ addicts
127 addOperations( aList );
130 aTypes.append( QVariant::Int );
131 aTypes.append( QVariant::UInt );
132 aTypes.append( QVariant::Double );
139 QtxArithmetics::~QtxArithmetics()
144 Creates numbers by it's string representation [redefined virtual]
146 bool QtxArithmetics::createValue( const QString& str, QtxValue& v ) const
149 v = str.toInt( &ok );
153 v = str.toDouble( &ok );
155 ok = QtxStdOperations::createValue( str, v );
161 \return priority of arithmetic operation 'op'.
162 \param isBin indicate whether the operation is binary
164 int QtxArithmetics::prior( const QString& op, bool isBin ) const
167 if( op=="<" || op==">" || op=="=" ||
168 op=="<=" || op==">=" || op=="<>" || op=="!=" )
170 else if( op=="+" || op=="-" )
172 else if( op=="*" || op=="/" )
176 else if( op=="+" || op=="-" )
182 void set( QVariant& v1, bool v2 )
184 v1 = QVariant( v2, 0 );
188 Calculates result of operation
189 \return one of error states
190 \param op - name of operation
191 \param v1 - first operation argument (must be used also to store result)
192 \param v2 - second operation argument
194 QtxParser::Error QtxArithmetics::calculate( const QString& op,
195 QtxValue& v1, QtxValue& v2 ) const
197 QtxParser::Error err = QtxParser::OK;
199 if( v1.isValid() && v2.isValid() )
201 if( ( v1.type()==QVariant::Int || v1.type()==QVariant::UInt ) &&
202 ( v2.type()==QVariant::Int || v2.type()==QVariant::UInt ) )
204 int _v1 = v1.toInt(),
218 v1 = double( _v1 ) / double( _v2 );
220 err = QtxParser::InvalidResult;
231 else if( op=="<>" || op=="!=" )
234 else if( ( v1.type()==QVariant::Int || v1.type()==QVariant::Double ) &&
235 ( v2.type()==QVariant::Int || v2.type()==QVariant::Double ) )
237 double _v1 = v1.toDouble(),
250 err = QtxParser::InvalidResult;
261 else if( op=="<>" || op=="!=" )
267 if( v2.type()==QVariant::Int )
269 else if( v2.type()==QVariant::Double )
292 aList.append( "and" );
293 aList.append( "&&" );
294 aList.append( "or" );
295 aList.append( "||" );
296 aList.append( "xor" );
297 aList.append( "not" );
299 aList.append( "imp" );
301 addOperations( aList );
304 aTypes.append( QVariant::Bool );
305 aTypes.append( QVariant::Int );
306 aTypes.append( QVariant::UInt );
313 QtxLogic::~QtxLogic()
318 Creates value 'true' or 'false' it's string representation [redefined virtual]
320 bool QtxLogic::createValue( const QString& str, QtxValue& v ) const
323 if( str.lower()=="true" )
324 v = QtxValue( true, 0 );
325 else if( str.lower()=="false" )
326 v = QtxValue( false, 0 );
328 ok = QtxStdOperations::createValue( str, v );
334 \return priority of arithmetic operation 'op'.
335 \param isBin indicate whether the operation is binary
337 int QtxLogic::prior( const QString& op, bool isBin ) const
340 if( op=="and" || op=="or" || op=="xor" ||
341 op=="&&" || op=="||" ||
348 else if( op=="not" || op=="!" )
354 bool boolean_value( const QtxValue& v )
356 if( v.type()==QVariant::Bool )
358 else if( v.type()==QVariant::Int )
360 else if( v.type()==QVariant::UInt )
361 return v.toUInt()!=0;
367 Calculates result of operation
368 \return one of error states
369 \param op - name of operation
370 \param v1 - first operation argument (must be used also to store result)
371 \param v2 - second operation argument
373 QtxParser::Error QtxLogic::calculate( const QString& op,
374 QtxValue& v1, QtxValue& v2 ) const
376 QtxParser::Error err = QtxParser::OK;
377 bool val1 = boolean_value( v1 ),
378 val2 = boolean_value( v2 );
379 if( v1.isValid() && v2.isValid() )
381 if( op=="and" || op=="&&" )
382 set( v1, val1 && val2 );
383 else if( op=="or" || op=="||" )
384 set( v1, val1 || val2 );
386 set( v1, ( !val1 && val2 ) || ( val1 && !val2 ) );
388 set( v1, !val1 || val2 );
390 set( v1, val1==val2 );
393 if( op=="not" || op=="!" )
407 QtxFunctions::QtxFunctions()
411 aList.append( "sqrt" );
412 aList.append( "abs" );
413 aList.append( "sin" );
414 aList.append( "cos" );
415 aList.append( "rad2grad" );
416 aList.append( "grad2rad" );
417 addOperations( aList );
420 aTypes.append( QVariant::Int );
421 aTypes.append( QVariant::Double );
428 QtxFunctions::~QtxFunctions()
433 Creates numbers by it's string representation [redefined virtual]
435 bool QtxFunctions::createValue( const QString& str, QtxValue& v ) const
438 v = str.toInt( &ok );
442 v = str.toDouble( &ok );
444 ok = QtxStdOperations::createValue( str, v );
450 \return priority of arithmetic operation 'op'.
451 \param isBin indicate whether the operation is binary
453 int QtxFunctions::prior( const QString& op, bool isBin ) const
457 else if( op=="sqrt" || op=="abs" || op=="sin" || op=="cos" ||
458 op=="rad2grad" || op=="grad2rad" )
465 Calculates result of operation
466 \return one of error states
467 \param op - name of operation
468 \param v1 - first operation argument (must be used also to store result)
469 \param v2 - second operation argument
471 QtxParser::Error QtxFunctions::calculate( const QString& op,
472 QtxValue& v1, QtxValue& v2 ) const
474 QtxParser::Error err = QtxParser::OK;
475 double val = v2.toDouble();
480 err = QtxParser::InvalidResult;
482 if( v2.type()==QVariant::Int )
483 v2 = abs( v2.toInt() );
485 v2 = fabs( v2.toDouble() );
490 else if( op=="grad2rad" )
491 v2 = val * 3.14159256 / 180.0;
492 else if( op=="rad2grad" )
493 v2 = val * 180.0 / 3.14159256;
508 QtxStrings::QtxStrings()
516 aList.append( "<=" );
517 aList.append( ">=" );
518 aList.append( "<>" );
519 aList.append( "!=" ); // same as "<>" - for C++ addicts
520 aList.append( "length" );
521 aList.append( "lower" );
522 aList.append( "upper" );
523 addOperations( aList );
526 aTypes.append( QVariant::Int );
527 aTypes.append( QVariant::Double );
528 aTypes.append( QVariant::String );
529 aTypes.append( QVariant::CString );
537 QtxStrings::~QtxStrings()
542 Creates string QtxValue by it's Qt string representation [redefined virtual]
544 bool QtxStrings::createValue( const QString& str, QtxValue& v ) const
547 fin = str[ ( int )( str.length()-1 ) ];
548 if( st=="'" && fin=="'" )
550 v = str.mid( 1, str.length()-2 );
554 return QtxStdOperations::createValue( str, v );
558 \return priority of arithmetic operation 'op'.
559 \param isBin indicate whether the operation is binary
561 int QtxStrings::prior( const QString& op, bool isBin ) const
566 else if( op=="=" || op=="<" || op==">" ||
567 op=="<=" || op==">=" || op=="<>" || op=="!=" )
572 if( op=="length" || op=="lower" || op=="upper" )
579 Calculates result of operation
580 \return one of error states
581 \param op - name of operation
582 \param v1 - first operation argument (must be used also to store result)
583 \param v2 - second operation argument
585 QtxParser::Error QtxStrings::calculate( const QString& op,
586 QtxValue& v1, QtxValue& v2 ) const
588 QtxParser::Error err = QtxParser::OK;
589 if( v1.isValid() && v2.isValid() )
591 QString _v1 = v1.toString(),
601 else if( op=="<>" || op=="!=" )
604 set( v1, _v1<_v2 || _v1==_v2 );
606 set( v1, _v1>_v2 || _v1==_v2 );
608 else if( !v1.isValid() && v2.isValid() )
610 QString val = v2.toString();
612 v2 = ( int )val.length();
613 else if( op=="lower" )
615 else if( op=="upper" )
636 aList.append( "<>" );
637 aList.append( "!=" ); // same as "<>" - for C++ addicts
641 aList.append( "in" );
642 aList.append( "count" );
643 addOperations( aList );
646 aTypes.append( QVariant::List );
658 Fills list of brackets treated as open (when 'open' is 'true') or close ('open' is 'false')
660 void QtxSets::bracketsList( QStringList& list, bool open ) const
666 QtxStdOperations::bracketsList( list, open );
670 Creates set of QtxValues by their string representation [redefined virtual]
672 bool QtxSets::createValue( const QString& str, QtxValue& val ) const
674 return QtxStdOperations::createValue( str, val );
678 \return priority of arithmetic operation 'op'.
679 \param isBin indicate whether the operation is binary
681 int QtxSets::prior( const QString& op, bool isBin ) const
684 if( op=="=" || op=="<>" || op=="!=" )
686 else if( op=="+" || op=="-" || op=="*" )
694 if( op=="{" || op=="}" )
696 else if( op=="count" )
703 \return whether values with passed types are valid for arguments of operation
704 \param op - name of operation
705 \param t1 - type of first argument
706 \param t2 - type of second argument
708 QtxParser::Error QtxSets::isValid( const QString& op,
709 const QVariant::Type t1,
710 const QVariant::Type t2 ) const
713 return QtxParser::OK;
715 if( t1!=QVariant::Invalid && t2==QVariant::List )
716 return QtxParser::OK;
718 return QtxParser::OperandsNotMatch;
720 return QtxStdOperations::isValid( op, t1, t2 );
724 Adds new value 'v' to set 'set' [static]
726 void QtxSets::add( ValueSet& set, const QtxValue& v )
728 if( v.isValid() && set.contains( v )==0 )
733 Adds values from set 's2' to set 's1'
735 void QtxSets::add( ValueSet& s1, const ValueSet& s2 )
737 ValueSet::const_iterator anIt = s2.begin(),
739 for( ; anIt!=aLast; anIt++ )
744 Removes value 'v' from set 'set'
746 void QtxSets::remove( ValueSet& set, const QtxValue& v )
752 Removes values of set 's2' from set 's1'
754 void QtxSets::remove( ValueSet& s1, const ValueSet& s2 )
756 ValueSet::const_iterator anIt = s2.begin(),
758 for( ; anIt!=aLast; anIt++ )
763 Calculates result of operation
764 \return one of error states
765 \param op - name of operation
766 \param v1 - first operation argument (must be used also to store result)
767 \param v2 - second operation argument
769 QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue& v2 ) const
771 QtxParser::Error err = QtxParser::OK;
777 add( aNewList, v1.toList() );
781 else if( op=="=" || op=="<>" || op=="!=" || op=="+" || op=="-" || op=="*" )
784 add( aNewList, v1.toList() );
786 if( op=="=" || op=="<>" || op=="!=" || op=="-" )
788 remove( aNewList, v2.toList() );
791 set( v1, aNewList.isEmpty() && v1.toList().count()==v2.toList().count() );
792 else if( op=="<>" || op=="!=" )
793 set( v1, !aNewList.isEmpty() || v1.toList().count()!=v2.toList().count() );
799 add( aNewList, v2.toList() );
805 add( toDelete, aNewList );
806 remove( toDelete, v2.toList() );
807 remove( aNewList, toDelete );
811 else if( op=="count" )
812 v2 = ( int )v2.toList().count();
815 if( v1.type()==QVariant::List )
818 ValueSet::const_iterator anIt = v1.toList().begin(),
819 aLast = v1.toList().end();
820 for( ; anIt!=aLast && res; anIt++ )
821 res = v2.toList().contains( *anIt )>0;
827 const QValueList< QVariant >& list = v2.toList();
828 v1 = ( bool )( list.find( v1 )!=list.end() );