Salome HOME
f61713c05ab8aaf98173c9f43d777d12a97e77ce
[modules/gui.git] / src / Qtx / QtxListOfOperations.cxx
1
2 #include "QtxListOfOperations.h"
3 #include <stdarg.h>
4
5 //================================================================
6 // Function : 
7 // Purpose  : 
8 //================================================================
9 QtxListOfOperations::QtxListOfOperations()
10 {
11 }
12
13 //================================================================
14 // Function : 
15 // Purpose  : 
16 //================================================================
17 QtxListOfOperations::~QtxListOfOperations()
18 {
19 }
20
21 //================================================================
22 // Function : 
23 // Purpose  : 
24 //================================================================
25 void QtxListOfOperations::bracketsList( QStringList& list, bool open ) const
26 {
27     OperationSetsIterator anIt = mySets.begin(),
28                           aLast = mySets.end();
29     QStringList custom;
30     for( ; anIt!=aLast; anIt++ )
31     {
32         custom.clear();
33         (*anIt).myOperations->bracketsList( custom, open );
34         QStringList::const_iterator aSIt = custom.begin(),
35                                     aSLast = custom.end();
36         for( ; aSIt!=aSLast; aSIt++ )
37             if( list.contains( *aSIt )==0 )
38                 list.append( *aSIt );
39     }
40 }
41
42 //================================================================
43 // Function : 
44 // Purpose  : 
45 //================================================================
46 void QtxListOfOperations::opersList( QStringList& list ) const
47 {
48     OperationSetsIterator anIt = mySets.begin(),
49                           aLast = mySets.end();
50     QStringList custom;
51     for( ; anIt!=aLast; anIt++ )
52     {
53         custom.clear();
54         (*anIt).myOperations->opersList( custom );
55         QStringList::const_iterator aSIt = custom.begin(),
56                                     aSLast = custom.end();
57         for( ; aSIt!=aSLast; aSIt++ )
58             if( list.contains( *aSIt )==0 )
59                 list.append( *aSIt );
60     }
61 }
62
63 //================================================================
64 // Function : 
65 // Purpose  : 
66 //================================================================
67 bool QtxListOfOperations::createValue( const QString& str, QtxValue& val ) const
68 {
69     bool ok;
70     OperationSetsIterator anIt = mySets.begin(),
71                           aLast = mySets.end();
72     for( ; anIt!=aLast; anIt++ )
73     {
74         ok = (*anIt).myOperations->createValue( str, val );
75         if( ok )
76             break;
77     }
78     return ok;
79 }
80
81 //================================================================
82 // Function : 
83 // Purpose  : 
84 //================================================================
85 int QtxListOfOperations::prior( const QString& op, bool isBin ) const
86 {
87     OperationSetsIterator anIt = mySets.begin(),
88                           aLast = mySets.end();
89     int prior = 0;
90     for( ; anIt!=aLast; anIt++ )
91     {
92         prior = (*anIt).myOperations->prior( op, isBin );
93         if( prior>0 )
94         {
95             prior+=(*anIt).myAddPrior;
96             break;
97         }
98     }
99     return prior>0 ? prior : 0;
100 }
101
102 //================================================================
103 // Function : 
104 // Purpose  : 
105 //================================================================
106 QtxParser::Error QtxListOfOperations::isValid( const QString& op,
107                                                const QVariant::Type t1,
108                                                const QVariant::Type t2 ) const
109 {
110     OperationSetsIterator anIt = mySets.begin(),
111                           aLast = mySets.end();
112     QtxParser::Error err = QtxParser::OK;
113     for( ; anIt!=aLast; anIt++ )
114     {
115         err = (*anIt).myOperations->isValid( op, t1, t2 );
116         if( err==QtxParser::OK )
117             break;
118     }
119     return err;
120 }
121
122 //================================================================
123 // Function : 
124 // Purpose  : 
125 //================================================================
126 QtxParser::Error QtxListOfOperations::calculate( const QString& op,
127                                                  QtxValue& v1,
128                                                  QtxValue& v2 ) const
129 {
130     const char* deb = op.latin1();
131
132     OperationSetsIterator anIt = mySets.begin(),
133                           aLast = mySets.end();
134     QtxValue nv1, nv2;
135     for( ; anIt!=aLast; anIt++ )
136     {
137         nv1 = v1;
138         nv2 = v2;
139         if( (*anIt).myOperations->isValid( op, v1.type(), v2.type() ) == QtxParser::OK )
140         {
141             QtxParser::Error err = (*anIt).myOperations->calculate( op, nv1, nv2 );
142             if( err==QtxParser::OK || err==QtxParser::InvalidResult )
143             {
144                 QString oop = (*anIt).myName;
145                 const char* ooo = oop.latin1();
146                 v1 = nv1; v2 = nv2; 
147                 return err;
148             }
149         }
150     }
151     return QtxParser::InvalidOperation;
152 }
153
154 //================================================================
155 // Function : 
156 // Purpose  : 
157 //================================================================
158 void QtxListOfOperations::clear()
159 {
160     mySets.clear();
161 }
162
163 //================================================================
164 // Function : 
165 // Purpose  : 
166 //================================================================
167 bool QtxListOfOperations::has( const QString& name ) const
168 {
169     OperationSetsIterator anIt = mySets.begin(),
170                           aLast = mySets.end();
171     for( ; anIt!=aLast; anIt++ )
172         if( (*anIt).myName == name )
173             return true;
174     return false;
175 }
176
177 //================================================================
178 // Function : 
179 // Purpose  : 
180 //================================================================
181 void QtxListOfOperations::append( const QString& name, QtxOperations* oper,
182                                   int prior )
183 {
184     insert( name, oper, prior );
185 }
186
187 //================================================================
188 // Function : 
189 // Purpose  : 
190 //================================================================
191 void QtxListOfOperations::prepend( const QString& name, QtxOperations* oper,
192                                    int prior )
193 {
194     insert( name, oper, prior, 0 );
195 }
196
197 //================================================================
198 // Function : 
199 // Purpose  : 
200 //================================================================
201 void QtxListOfOperations::insert( const QString& name, QtxOperations* oper,
202                                   int prior, int pos )
203 {
204     if( has( name ) || oper==NULL || prior<0 )
205         return;
206
207     OperationSet op;
208     op.myName = name;
209     op.myOperations = oper;
210     op.myAddPrior = prior;
211     if( pos<0 )
212         mySets.append( op );
213     else
214         mySets.insert( mySets.at( pos ), op );
215 }
216
217 //================================================================
218 // Function : 
219 // Purpose  : 
220 //================================================================
221 void QtxListOfOperations::remove( const QString& name )
222 {
223     OperationSets::iterator anIt = mySets.begin(),
224                             aLast = mySets.end();
225     for( ; anIt!=aLast; anIt++ )
226         if( (*anIt).myName == name )
227         {
228             mySets.erase( anIt );
229             break;
230         }
231 }
232
233 //================================================================
234 // Function : 
235 // Purpose  : 
236 //================================================================
237 int QtxListOfOperations::count() const
238 {
239     return mySets.count();
240 }
241
242 //================================================================
243 // Function : 
244 // Purpose  : 
245 //================================================================
246 QtxOperations* QtxListOfOperations::operations( const QString& name ) const
247 {
248     OperationSetsIterator anIt = mySets.begin(),
249                           aLast = mySets.end();
250     for( ; anIt!=aLast; anIt++ )
251         if( (*anIt).myName == name )
252             return (*anIt).myOperations;
253     return NULL;
254 }