]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxListOfOperations.cxx
Salome HOME
39eb5f1a2a7c7c4ae317c64960cddcd476f7b10e
[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   Constructor
25 */
26 QtxListOfOperations::QtxListOfOperations()
27 {
28 }
29
30 /*!
31   Destructor
32 */
33 QtxListOfOperations::~QtxListOfOperations()
34 {
35 }
36
37 /*!
38   \return fills list with brackets symbols
39   \param list - list to be filled
40   \param open - if it is true, it is necessary to fill with open brackets otherwise with close
41 */
42 void QtxListOfOperations::bracketsList( QStringList& list, bool open ) const
43 {
44     OperationSetsIterator anIt = mySets.begin(),
45                           aLast = mySets.end();
46     QStringList custom;
47     for( ; anIt!=aLast; anIt++ )
48     {
49         custom.clear();
50         (*anIt).myOperations->bracketsList( custom, open );
51         QStringList::const_iterator aSIt = custom.begin(),
52                                     aSLast = custom.end();
53         for( ; aSIt!=aSLast; aSIt++ )
54             if( list.contains( *aSIt )==0 )
55                 list.append( *aSIt );
56     }
57 }
58
59 /*!
60   Fills list with operation names
61 */
62 void QtxListOfOperations::opersList( QStringList& list ) const
63 {
64     OperationSetsIterator anIt = mySets.begin(),
65                           aLast = mySets.end();
66     QStringList custom;
67     for( ; anIt!=aLast; anIt++ )
68     {
69         custom.clear();
70         (*anIt).myOperations->opersList( custom );
71         QStringList::const_iterator aSIt = custom.begin(),
72                                     aSLast = custom.end();
73         for( ; aSIt!=aSLast; aSIt++ )
74             if( list.contains( *aSIt )==0 )
75                 list.append( *aSIt );
76     }
77 }
78
79 /*!
80   Create some value by it's string representation
81 */
82 bool QtxListOfOperations::createValue( const QString& str, QtxValue& val ) const
83 {
84     bool ok;
85     OperationSetsIterator anIt = mySets.begin(),
86                           aLast = mySets.end();
87     for( ; anIt!=aLast; anIt++ )
88     {
89         ok = (*anIt).myOperations->createValue( str, val );
90         if( ok )
91             break;
92     }
93     return ok;
94 }
95
96 /*!
97   \return priority of operation
98 */
99 int QtxListOfOperations::prior( const QString& op, bool isBin ) const
100 {
101     OperationSetsIterator anIt = mySets.begin(),
102                           aLast = mySets.end();
103     int prior = 0;
104     for( ; anIt!=aLast; anIt++ )
105     {
106         prior = (*anIt).myOperations->prior( op, isBin );
107         if( prior>0 )
108         {
109             prior+=(*anIt).myAddPrior;
110             break;
111         }
112     }
113     return prior>0 ? prior : 0;
114 }
115
116 /*!
117    \return whether values with passed types are valid for arguments of operation
118    \param op - name of operation
119    \param t1 - type of first argument
120    \param t2 - type of second argument
121 */
122 QtxParser::Error QtxListOfOperations::isValid( const QString& op,
123                                                const QVariant::Type t1,
124                                                const QVariant::Type t2 ) const
125 {
126     OperationSetsIterator anIt = mySets.begin(),
127                           aLast = mySets.end();
128     QtxParser::Error err = QtxParser::OK;
129     for( ; anIt!=aLast; anIt++ )
130     {
131         err = (*anIt).myOperations->isValid( op, t1, t2 );
132         if( err==QtxParser::OK )
133             break;
134     }
135     return err;
136 }
137
138 /*!
139     Calculates result of operation
140     \return one of error states
141     \param op - name of operation
142     \param v1 - first operation argument (must be used also to store result)
143     \param v2 - second operation argument
144 */
145 QtxParser::Error QtxListOfOperations::calculate( const QString& op,
146                                                  QtxValue& v1,
147                                                  QtxValue& v2 ) const
148 {
149     const char* deb = op.latin1();
150
151     OperationSetsIterator anIt = mySets.begin(),
152                           aLast = mySets.end();
153     QtxValue nv1, nv2;
154     for( ; anIt!=aLast; anIt++ )
155     {
156         nv1 = v1;
157         nv2 = v2;
158         if( (*anIt).myOperations->isValid( op, v1.type(), v2.type() ) == QtxParser::OK )
159         {
160             QtxParser::Error err = (*anIt).myOperations->calculate( op, nv1, nv2 );
161             if( err==QtxParser::OK || err==QtxParser::InvalidResult )
162             {
163                 QString oop = (*anIt).myName;
164                 const char* ooo = oop.latin1();
165                 v1 = nv1; v2 = nv2; 
166                 return err;
167             }
168         }
169     }
170     return QtxParser::InvalidOperation;
171 }
172
173 /*!
174   Clears list of operations
175 */
176 void QtxListOfOperations::clear()
177 {
178     mySets.clear();
179 }
180
181 /*!
182   \return true if list contains operations
183   \param name - name of operation
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   Appends operations to list
197   \param name - name of operations
198   \param oper - operations
199   \param prior - additional prior
200 */
201 void QtxListOfOperations::append( const QString& name, QtxOperations* oper,
202                                   int prior )
203 {
204     insert( name, oper, prior );
205 }
206
207 /*!
208   Prepends operations to list
209   \param name - name of operations
210   \param oper - operations
211   \param prior - additional prior
212 */
213 void QtxListOfOperations::prepend( const QString& name, QtxOperations* oper,
214                                    int prior )
215 {
216     insert( name, oper, prior, 0 );
217 }
218
219 /*!
220   Inserts operations to list
221   \param name - name of operations
222   \param oper - operations
223   \param prior - additional prior
224   \param pos - insert position 
225 */
226 void QtxListOfOperations::insert( const QString& name, QtxOperations* oper,
227                                   int prior, int pos )
228 {
229     if( has( name ) || oper==NULL || prior<0 )
230         return;
231
232     OperationSet op;
233     op.myName = name;
234     op.myOperations = oper;
235     op.myAddPrior = prior;
236     if( pos<0 )
237         mySets.append( op );
238     else
239         mySets.insert( mySets.at( pos ), op );
240 }
241
242 /*!
243   Removes operations from list
244   \param name - name of operations
245 */
246 void QtxListOfOperations::remove( const QString& name )
247 {
248     OperationSets::iterator anIt = mySets.begin(),
249                             aLast = mySets.end();
250     for( ; anIt!=aLast; anIt++ )
251         if( (*anIt).myName == name )
252         {
253             mySets.erase( anIt );
254             break;
255         }
256 }
257
258 /*!
259   \return count of operations in list
260 */
261 int QtxListOfOperations::count() const
262 {
263     return mySets.count();
264 }
265
266 /*!
267   \return operations by name
268   \name - name of operations
269 */
270 QtxOperations* QtxListOfOperations::operations( const QString& name ) const
271 {
272     OperationSetsIterator anIt = mySets.begin(),
273                           aLast = mySets.end();
274     for( ; anIt!=aLast; anIt++ )
275         if( (*anIt).myName == name )
276             return (*anIt).myOperations;
277     return NULL;
278 }