1 #include "QtxStdOperations.h"
6 //================================================================
9 //================================================================
10 QtxStdOperations::QtxStdOperations()
14 //================================================================
17 //================================================================
18 QtxStdOperations::~QtxStdOperations()
22 //================================================================
25 //================================================================
26 void QtxStdOperations::bracketsList( QStringList& list, bool open ) const
34 //================================================================
37 //================================================================
38 void QtxStdOperations::opersList( QStringList& list ) const
43 //================================================================
46 //================================================================
47 void QtxStdOperations::addOperations( const QStringList& list )
49 QStringList::const_iterator anIt = list.begin(),
51 for( ; anIt!=aLast; anIt++ )
52 if( myOpers.contains( *anIt )==0 )
53 myOpers.append( *anIt );
56 //================================================================
59 //================================================================
60 void QtxStdOperations::addTypes( const ListOfTypes& list )
62 ListOfTypes::const_iterator anIt = list.begin(),
64 for( ; anIt!=aLast; anIt++ )
65 if( myTypes.contains( *anIt )==0 )
66 myTypes.append( *anIt );
69 //================================================================
72 //================================================================
73 QtxParser::Error QtxStdOperations::isValid( const QString& op,
74 const QVariant::Type t1,
75 const QVariant::Type t2 ) const
77 if( ( t1==QVariant::Invalid || myTypes.contains( t1 ) ) &&
78 ( t2==QVariant::Invalid || myTypes.contains( t2 ) ) &&
79 ( t1!=QVariant::Invalid || t2!=QVariant::Invalid ) )
80 if( prior( op, t1!=QVariant::Invalid && t2!=QVariant::Invalid ) > 0 )
83 return QtxParser::InvalidOperation;
85 return QtxParser::OperandsNotMatch;
95 //================================================================
98 //================================================================
99 QtxArithmetics::QtxArithmetics()
110 aList.append( "<=" );
111 aList.append( ">=" );
112 aList.append( "<>" );
113 addOperations( aList );
116 aTypes.append( QVariant::Int );
117 aTypes.append( QVariant::UInt );
118 aTypes.append( QVariant::Double );
122 //================================================================
125 //================================================================
126 QtxArithmetics::~QtxArithmetics()
130 //================================================================
133 //================================================================
134 bool QtxArithmetics::createValue( const QString& str, QtxValue& v ) const
137 v = str.toInt( &ok );
141 v = str.toDouble( &ok );
143 ok = QtxStdOperations::createValue( str, v );
148 //================================================================
151 //================================================================
152 int QtxArithmetics::prior( const QString& op, bool isBin ) const
155 if( op=="<" || op==">" || op=="=" ||
156 op=="<=" || op==">=" || op=="<>" )
158 else if( op=="+" || op=="-" )
160 else if( op=="*" || op=="/" )
164 else if( op=="+" || op=="-" )
170 void set( QVariant& v1, bool v2 )
172 v1 = QVariant( v2, 0 );
175 //================================================================
178 //================================================================
179 QtxParser::Error QtxArithmetics::calculate( const QString& op,
180 QtxValue& v1, QtxValue& v2 ) const
182 QtxParser::Error err = QtxParser::OK;
184 if( v1.isValid() && v2.isValid() )
186 if( ( v1.type()==QVariant::Int || v1.type()==QVariant::UInt ) &&
187 ( v2.type()==QVariant::Int || v2.type()==QVariant::UInt ) )
189 int _v1 = v1.toInt(),
203 v1 = double( _v1 ) / double( _v2 );
205 err = QtxParser::InvalidResult;
219 else if( ( v1.type()==QVariant::Int || v1.type()==QVariant::Double ) &&
220 ( v2.type()==QVariant::Int || v2.type()==QVariant::Double ) )
222 double _v1 = v1.toDouble(),
235 err = QtxParser::InvalidResult;
252 if( v2.type()==QVariant::Int )
254 else if( v2.type()==QVariant::Double )
270 //================================================================
273 //================================================================
278 aList.append( "and" );
279 aList.append( "&&" );
280 aList.append( "or" );
281 aList.append( "||" );
282 aList.append( "xor" );
283 aList.append( "not" );
285 aList.append( "imp" );
287 addOperations( aList );
290 aTypes.append( QVariant::Bool );
294 //================================================================
297 //================================================================
298 QtxLogic::~QtxLogic()
302 //================================================================
305 //================================================================
306 bool QtxLogic::createValue( const QString& str, QtxValue& v ) const
309 if( str.lower()=="true" )
311 else if( str.lower()=="false" )
314 ok = QtxStdOperations::createValue( str, v );
319 //================================================================
322 //================================================================
323 int QtxLogic::prior( const QString& op, bool isBin ) const
326 if( op=="and" || op=="or" || op=="xor" ||
327 op=="&&" || op=="||" ||
334 else if( op=="not" || op=="!" )
340 //================================================================
343 //================================================================
344 QtxParser::Error QtxLogic::calculate( const QString& op,
345 QtxValue& v1, QtxValue& v2 ) const
347 QtxParser::Error err = QtxParser::OK;
348 if( v1.isValid() && v2.isValid() )
350 if( op=="and" || op=="&&" )
351 set( v1, v1.toBool() && v2.toBool() );
352 else if( op=="or" || op=="||" )
353 set( v1, v1.toBool() || v2.toBool() );
355 set( v1, ( !v1.toBool() && v2.toBool() ) || ( v1.toBool() && !v2.toBool() ) );
357 set( v1, !v1.toBool() || v2.toBool() );
359 set( v1, v1.toBool()==v2.toBool() );
362 if( op=="not" || op=="!" )
373 //================================================================
376 //================================================================
377 QtxFunctions::QtxFunctions()
381 aList.append( "sqrt" );
382 aList.append( "abs" );
383 aList.append( "sin" );
384 aList.append( "cos" );
385 aList.append( "rad2grad" );
386 aList.append( "grad2rad" );
387 addOperations( aList );
390 aTypes.append( QVariant::Int );
391 aTypes.append( QVariant::Double );
395 //================================================================
398 //================================================================
399 QtxFunctions::~QtxFunctions()
403 //================================================================
406 //================================================================
407 bool QtxFunctions::createValue( const QString& str, QtxValue& v ) const
410 v = str.toInt( &ok );
414 v = str.toDouble( &ok );
416 ok = QtxStdOperations::createValue( str, v );
421 //================================================================
424 //================================================================
425 int QtxFunctions::prior( const QString& op, bool isBin ) const
429 else if( op=="sqrt" || op=="abs" || op=="sin" || op=="cos" ||
430 op=="rad2grad" || op=="grad2rad" )
436 //================================================================
439 //================================================================
440 QtxParser::Error QtxFunctions::calculate( const QString& op,
441 QtxValue& v1, QtxValue& v2 ) const
443 QtxParser::Error err = QtxParser::OK;
444 double val = v2.toDouble();
449 err = QtxParser::InvalidResult;
451 if( v2.type()==QVariant::Int )
452 v2 = abs( v2.toInt() );
454 v2 = fabs( v2.toDouble() );
459 else if( op=="grad2rad" )
460 v2 = val * 3.14159256 / 180.0;
461 else if( op=="rad2grad" )
462 v2 = val * 180.0 / 3.14159256;
474 //================================================================
477 //================================================================
478 QtxStrings::QtxStrings()
486 aList.append( "<=" );
487 aList.append( ">=" );
488 aList.append( "<>" );
489 aList.append( "length" );
490 aList.append( "lower" );
491 aList.append( "upper" );
492 addOperations( aList );
495 aTypes.append( QVariant::Int );
496 aTypes.append( QVariant::Double );
497 aTypes.append( QVariant::String );
498 aTypes.append( QVariant::CString );
503 //================================================================
506 //================================================================
507 QtxStrings::~QtxStrings()
511 //================================================================
514 //================================================================
515 bool QtxStrings::createValue( const QString& str, QtxValue& v ) const
518 fin = str[ ( int )( str.length()-1 ) ];
519 if( st=="'" && fin=="'" )
521 v = str.mid( 1, str.length()-2 );
525 return QtxStdOperations::createValue( str, v );
528 //================================================================
531 //================================================================
532 int QtxStrings::prior( const QString& op, bool isBin ) const
537 else if( op=="=" || op=="<" || op==">" ||
538 op=="<=" || op==">=" || op=="<>" )
543 if( op=="length" || op=="lower" || op=="upper" )
549 //================================================================
552 //================================================================
553 QtxParser::Error QtxStrings::calculate( const QString& op,
554 QtxValue& v1, QtxValue& v2 ) const
556 QtxParser::Error err = QtxParser::OK;
557 if( v1.isValid() && v2.isValid() )
559 QString _v1 = v1.toString(),
572 set( v1, _v1<_v2 || _v1==_v2 );
574 set( v1, _v1>_v2 || _v1==_v2 );
576 else if( !v1.isValid() && v2.isValid() )
578 QString val = v2.toString();
580 v2 = ( int )val.length();
581 else if( op=="lower" )
583 else if( op=="upper" )
595 //================================================================
598 //================================================================
605 aList.append( "<>" );
609 aList.append( "in" );
610 aList.append( "count" );
611 addOperations( aList );
614 aTypes.append( QVariant::List );
618 //================================================================
621 //================================================================
626 //================================================================
629 //================================================================
630 void QtxSets::bracketsList( QStringList& list, bool open ) const
636 QtxStdOperations::bracketsList( list, open );
639 //================================================================
642 //================================================================
643 bool QtxSets::createValue( const QString& str, QtxValue& val ) const
645 return QtxStdOperations::createValue( str, val );
648 //================================================================
651 //================================================================
652 int QtxSets::prior( const QString& op, bool isBin ) const
655 if( op=="=" || op=="<>" )
657 else if( op=="+" || op=="-" || op=="*" )
665 if( op=="{" || op=="}" )
667 else if( op=="count" )
673 //================================================================
676 //================================================================
677 QtxParser::Error QtxSets::isValid( const QString& op,
678 const QVariant::Type t1,
679 const QVariant::Type t2 ) const
682 return QtxParser::OK;
684 if( t1!=QVariant::Invalid && t2==QVariant::List )
685 return QtxParser::OK;
687 return QtxParser::OperandsNotMatch;
689 return QtxStdOperations::isValid( op, t1, t2 );
692 //================================================================
695 //================================================================
696 void QtxSets::add( ValueSet& set, const QtxValue& v )
698 if( v.isValid() && set.contains( v )==0 )
702 //================================================================
705 //================================================================
706 void QtxSets::add( ValueSet& s1, const ValueSet& s2 )
708 ValueSet::const_iterator anIt = s2.begin(),
710 for( ; anIt!=aLast; anIt++ )
714 //================================================================
717 //================================================================
718 void QtxSets::remove( ValueSet& set, const QtxValue& v )
723 //================================================================
726 //================================================================
727 void QtxSets::remove( ValueSet& s1, const ValueSet& s2 )
729 ValueSet::const_iterator anIt = s2.begin(),
731 for( ; anIt!=aLast; anIt++ )
735 //================================================================
738 //================================================================
739 QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue& v2 ) const
741 QtxParser::Error err = QtxParser::OK;
747 add( aNewList, v1.toList() );
751 else if( op=="=" || op=="<>" || op=="+" || op=="-" || op=="*" )
754 add( aNewList, v1.toList() );
756 if( op=="=" || op=="<>" || op=="-" )
758 remove( aNewList, v2.toList() );
761 set( v1, aNewList.isEmpty() );
763 set( v1, !aNewList.isEmpty() );
769 add( aNewList, v2.toList() );
775 add( toDelete, aNewList );
776 remove( toDelete, v2.toList() );
777 remove( aNewList, toDelete );
781 else if( op=="count" )
782 v2 = ( int )v2.toList().count();
785 if( v1.type()==QVariant::List )
788 ValueSet::const_iterator anIt = v1.toList().begin(),
789 aLast = v1.toList().end();
790 for( ; anIt!=aLast && res; anIt++ )
791 res = v2.toList().contains( *anIt )>0;
796 v1 = v2.toList().contains( v1 );