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() )
349 if( op=="and" || op=="&&" )
350 set( v1, v1.toBool() && v2.toBool() );
351 else if( op=="or" || op=="||" )
352 set( v1, v1.toBool() || v2.toBool() );
354 set( v1, ( !v1.toBool() && v2.toBool() ) || ( v1.toBool() && !v2.toBool() ) );
356 set( v1, !v1.toBool() || v2.toBool() );
358 set( v1, v1.toBool()==v2.toBool() );
361 if( op=="not" || op=="!" )
372 //================================================================
375 //================================================================
376 QtxFunctions::QtxFunctions()
380 aList.append( "sqrt" );
381 aList.append( "abs" );
382 aList.append( "sin" );
383 aList.append( "cos" );
384 aList.append( "rad2grad" );
385 aList.append( "grad2rad" );
386 addOperations( aList );
389 aTypes.append( QVariant::Int );
390 aTypes.append( QVariant::Double );
394 //================================================================
397 //================================================================
398 QtxFunctions::~QtxFunctions()
402 //================================================================
405 //================================================================
406 bool QtxFunctions::createValue( const QString& str, QtxValue& v ) const
409 v = str.toInt( &ok );
413 v = str.toDouble( &ok );
415 ok = QtxStdOperations::createValue( str, v );
420 //================================================================
423 //================================================================
424 int QtxFunctions::prior( const QString& op, bool isBin ) const
428 else if( op=="sqrt" || op=="abs" || op=="sin" || op=="cos" ||
429 op=="rad2grad" || op=="grad2rad" )
435 //================================================================
438 //================================================================
439 QtxParser::Error QtxFunctions::calculate( const QString& op,
440 QtxValue& v1, QtxValue& v2 ) const
442 QtxParser::Error err = QtxParser::OK;
443 double val = v2.toDouble();
448 err = QtxParser::InvalidResult;
450 if( v2.type()==QVariant::Int )
451 v2 = abs( v2.toInt() );
453 v2 = fabs( v2.toDouble() );
458 else if( op=="grad2rad" )
459 v2 = val * 3.14159256 / 180.0;
460 else if( op=="rad2grad" )
461 v2 = val * 180.0 / 3.14159256;
473 //================================================================
476 //================================================================
477 QtxStrings::QtxStrings()
485 aList.append( "<=" );
486 aList.append( ">=" );
487 aList.append( "<>" );
488 aList.append( "length" );
489 aList.append( "lower" );
490 aList.append( "upper" );
491 addOperations( aList );
494 aTypes.append( QVariant::Int );
495 aTypes.append( QVariant::Double );
496 aTypes.append( QVariant::String );
497 aTypes.append( QVariant::CString );
502 //================================================================
505 //================================================================
506 QtxStrings::~QtxStrings()
510 //================================================================
513 //================================================================
514 bool QtxStrings::createValue( const QString& str, QtxValue& v ) const
517 fin = str[ ( int )( str.length()-1 ) ];
518 if( st=="'" && fin=="'" )
520 v = str.mid( 1, str.length()-2 );
524 return QtxStdOperations::createValue( str, v );
527 //================================================================
530 //================================================================
531 int QtxStrings::prior( const QString& op, bool isBin ) const
536 else if( op=="=" || op=="<" || op==">" ||
537 op=="<=" || op==">=" || op=="<>" )
542 if( op=="length" || op=="lower" || op=="upper" )
548 //================================================================
551 //================================================================
552 QtxParser::Error QtxStrings::calculate( const QString& op,
553 QtxValue& v1, QtxValue& v2 ) const
555 QtxParser::Error err = QtxParser::OK;
556 if( v1.isValid() && v2.isValid() )
558 QString _v1 = v1.toString(),
571 set( v1, _v1<_v2 || _v1==_v2 );
573 set( v1, _v1>_v2 || _v1==_v2 );
575 else if( !v1.isValid() && v2.isValid() )
577 QString val = v2.toString();
579 v2 = ( int )val.length();
580 else if( op=="lower" )
582 else if( op=="upper" )
594 //================================================================
597 //================================================================
604 aList.append( "<>" );
608 aList.append( "in" );
609 aList.append( "count" );
610 addOperations( aList );
613 aTypes.append( QVariant::List );
617 //================================================================
620 //================================================================
625 //================================================================
628 //================================================================
629 void QtxSets::bracketsList( QStringList& list, bool open ) const
635 QtxStdOperations::bracketsList( list, open );
638 //================================================================
641 //================================================================
642 bool QtxSets::createValue( const QString& str, QtxValue& val ) const
644 return QtxStdOperations::createValue( str, val );
647 //================================================================
650 //================================================================
651 int QtxSets::prior( const QString& op, bool isBin ) const
654 if( op=="=" || op=="<>" )
656 else if( op=="+" || op=="-" || op=="*" )
664 if( op=="{" || op=="}" )
666 else if( op=="count" )
672 //================================================================
675 //================================================================
676 QtxParser::Error QtxSets::isValid( const QString& op,
677 const QVariant::Type t1,
678 const QVariant::Type t2 ) const
681 return QtxParser::OK;
683 if( t1!=QVariant::Invalid && t2==QVariant::List )
684 return QtxParser::OK;
686 return QtxParser::OperandsNotMatch;
688 return QtxStdOperations::isValid( op, t1, t2 );
691 //================================================================
694 //================================================================
695 void QtxSets::add( ValueSet& set, const QtxValue& v )
697 if( v.isValid() && set.contains( v )==0 )
701 //================================================================
704 //================================================================
705 void QtxSets::add( ValueSet& s1, const ValueSet& s2 )
707 ValueSet::const_iterator anIt = s2.begin(),
709 for( ; anIt!=aLast; anIt++ )
713 //================================================================
716 //================================================================
717 void QtxSets::remove( ValueSet& set, const QtxValue& v )
722 //================================================================
725 //================================================================
726 void QtxSets::remove( ValueSet& s1, const ValueSet& s2 )
728 ValueSet::const_iterator anIt = s2.begin(),
730 for( ; anIt!=aLast; anIt++ )
734 //================================================================
737 //================================================================
738 QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue& v2 ) const
740 QtxParser::Error err = QtxParser::OK;
746 add( aNewList, v1.toList() );
750 else if( op=="=" || op=="<>" || op=="+" || op=="-" || op=="*" )
753 add( aNewList, v1.toList() );
755 if( op=="=" || op=="<>" || op=="-" )
757 remove( aNewList, v2.toList() );
760 set( v1, aNewList.isEmpty() );
762 set( v1, !aNewList.isEmpty() );
768 add( aNewList, v2.toList() );
774 add( toDelete, aNewList );
775 remove( toDelete, v2.toList() );
776 remove( aNewList, toDelete );
780 else if( op=="count" )
781 v2 = ( int )v2.toList().count();
784 if( v1.type()==QVariant::List )
787 ValueSet::const_iterator anIt = v1.toList().begin(),
788 aLast = v1.toList().end();
789 for( ; anIt!=aLast && res; anIt++ )
790 res = v2.toList().contains( *anIt )>0;
795 v1 = v2.toList().contains( v1 );