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