1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
20 #include "QtxPopupMgr.h"
21 #include "QtxListOfOperations.h"
22 #include "QtxStdOperations.h"
23 #include "QtxAction.h"
25 #include <qpopupmenu.h>
27 //================================================================
30 //================================================================
31 QtxValue QtxPopupMgr::Selection::globalParam( const QString& str ) const
33 if( str==selCountParam() )
36 else if( str[0]==equality() )
38 QtxSets::ValueSet set;
39 QString par = str.mid( 1 );
41 for( int i=0, n=count(); i<n; i++ )
43 QtxValue v = param( i, par );
45 QtxSets::add( set, v );
56 //================================================================
59 //================================================================
60 QChar QtxPopupMgr::Selection::equality() const
65 //================================================================
68 //================================================================
69 QString QtxPopupMgr::Selection::selCountParam() const
71 return defSelCountParam();
74 //================================================================
77 //================================================================
78 QChar QtxPopupMgr::Selection::defEquality()
83 //================================================================
86 //================================================================
87 QString QtxPopupMgr::Selection::defSelCountParam()
95 //================================================================
98 //================================================================
99 QtxPopupMgr::Operations::Operations( QtxPopupMgr* mgr )
104 aList.append( "every" );
105 aList.append( "any" );
106 aList.append( "onlyone" );
107 addOperations( aList );
109 myParser = new QtxParser( mgr->myOperations );
112 //================================================================
115 //================================================================
116 QtxPopupMgr::Operations::~Operations()
121 //================================================================
124 //================================================================
125 int QtxPopupMgr::Operations::prior( const QString& op, bool isBin ) const
127 if( !isBin && ( op=="every" || op=="any" || op=="onlyone" ) )
130 return QtxStrings::prior( op, isBin );
134 //================================================================
137 //================================================================
138 QtxParser::Error QtxPopupMgr::Operations::calculate
139 ( const QString& op, QtxValue& v1, QtxValue& v2 ) const
146 else if( op=="onlyone" )
149 if( ind>=0 && ind<=2 )
151 QString val_name = op + "(" + v2.toString() + ")";
152 QtxParser::Error err = QtxParser::OK;
154 if( !myValues.contains( val_name ) )
156 myParser->setExpr( v2.toString() );
157 QStringList params, specific;
158 myParser->paramsList( params );
161 myPopupMgr->setParams( myParser, specific );
163 QtxPopupMgr::Selection* sel = myPopupMgr->myCurrentSelection;
165 int global_result = 0;
167 for( int i=0, n=sel->count(); i<n; i++ )
169 QStringList::const_iterator anIt = specific.begin(),
170 aLast = specific.end();
171 for( ; anIt!=aLast; anIt++ )
173 QtxValue v = sel->param( i, *anIt );
175 myParser->set( *anIt, v );
177 return QtxParser::InvalidToken;
180 QtxValue res = myParser->calculate();
181 err = myParser->lastError();
182 if( err==QtxParser::OK )
183 if( res.type()==QVariant::Bool )
187 if( ind==2 && global_result>1 )
191 return QtxParser::InvalidResult;
196 QtxValue& vv = ( QtxValue& )myValues[ val_name ];
197 vv = ( ind==0 && global_result==sel->count() ) ||
199 ( ind==2 && global_result==1 );
202 v2 = myValues[ val_name ];
207 return QtxStrings::calculate( op, v1, v2 );
210 //================================================================
213 //================================================================
214 void QtxPopupMgr::Operations::clear()
227 //================================================================
230 //================================================================
231 QtxPopupMgr::QtxPopupMgr( QPopupMenu* popup, QObject* parent )
232 : QtxActionMenuMgr( popup, parent ),
233 myCurrentSelection( 0 )
238 //================================================================
241 //================================================================
242 QtxPopupMgr::~QtxPopupMgr()
246 //================================================================
249 //================================================================
250 void QtxPopupMgr::createOperations()
252 myOperations = new QtxListOfOperations;
253 myOperations->prepend( "logic", new QtxLogic(), 0 );
254 myOperations->prepend( "arithm", new QtxArithmetics(), 50 );
255 myOperations->append( "strings", new QtxStrings(), 100 );
256 myOperations->append( "sets", new QtxSets(), 150 );
257 myOperations->append( "custom", new Operations( this ), 200 );
260 //================================================================
263 //================================================================
264 int QtxPopupMgr::registerAction( QAction* act,
265 const QString& visible,
266 const QString& toggle,
269 int _id = QtxActionMenuMgr::registerAction( act, id );
270 setRule( _id, visible, true );
271 setRule( _id, toggle, false );
275 //================================================================
278 //================================================================
279 void QtxPopupMgr::unRegisterAction( const int id )
281 QAction* act = action( id );
283 myVisibility.remove( act );
284 myToggle.remove( act );
287 //QtxActionMenuMgr::unRegisterAction( id );
290 //================================================================
293 //================================================================
294 bool QtxPopupMgr::hasRule( QAction* act, bool visibility ) const
296 return map( visibility ).contains( act );
299 //================================================================
302 //================================================================
303 bool QtxPopupMgr::hasRule( const int id, bool visibility ) const
305 return hasRule( action( id ), visibility );
308 //================================================================
311 //================================================================
312 void QtxPopupMgr::setRule( QAction* act, const QString& rule, bool visibility )
314 if( !act || rule.isEmpty() )
317 if( !hasRule( act, visibility ) )
319 QtxParser* p = new QtxParser( myOperations, rule );
320 if( p->lastError()==QtxParser::OK )
321 map( visibility ).insert( act, p );
327 QtxParser* p = map( visibility )[ act ];
329 if( p->lastError()!=QtxParser::OK )
330 p->setExpr( QString() );
334 //================================================================
337 //================================================================
338 void QtxPopupMgr::setRule( const int id, const QString& rule, bool visibility )
340 setRule( action( id ), rule, visibility );
343 //================================================================
346 //================================================================
347 bool result( QtxParser* p )
352 QtxValue vv = p->calculate();
353 res = p->lastError()==QtxParser::OK &&
354 ( ( vv.type()==QVariant::Int && vv.toInt()!=0 ) ||
355 ( vv.type()==QVariant::Bool && vv.toBool() ) );
360 //================================================================
363 //================================================================
364 void QtxPopupMgr::setParams( QtxParser* p, QStringList& specific ) const
366 if( !p || !myCurrentSelection )
371 p->paramsList( params );
372 QStringList::const_iterator anIt = params.begin(),
373 aLast = params.end();
374 for( ; anIt!=aLast; anIt++ )
376 QtxValue v = myCurrentSelection->globalParam( *anIt );
380 specific.append( *anIt );
384 //================================================================
387 //================================================================
388 bool QtxPopupMgr::isSatisfied( QAction* act, bool visibility ) const
390 QString menu = act->menuText();
391 //const char* mmm = menu.latin1();
397 if( hasRule( act, visibility ) )
399 QtxParser* p = map( visibility )[ act ];
400 //QString pdump = p->dump();
401 //const char* pdd = pdump.latin1();
404 QStringList specific;
406 ( ( Operations* )myOperations->operations( "custom" ) )->clear();
408 setParams( p, specific );
410 if( specific.count()>0 )
411 if( myCurrentSelection )
412 for( int i=0, n=myCurrentSelection->count(); i<n; i++ )
414 QStringList::const_iterator anIt1 = specific.begin(),
415 aLast1 = specific.end();
416 for( ; anIt1!=aLast1; anIt1++ )
417 p->set( *anIt1, myCurrentSelection->param( i, *anIt1 ) );
419 res = res || result( p );
430 //================================================================
433 //================================================================
434 bool QtxPopupMgr::isVisible( const int actId, const int place ) const
436 bool res = QtxActionMenuMgr::isVisible( actId, place );
437 QAction* act = action( actId );
438 if( hasRule( act, true ) )
439 res = res && isSatisfied( act, true );
443 //================================================================
446 //================================================================
447 void QtxPopupMgr::updatePopup( QPopupMenu* p, Selection* sel )
452 myCurrentSelection = sel;
454 RulesMap::iterator anIt = myToggle.begin(),
455 aLast = myToggle.end();
456 for( ; anIt!=aLast; anIt++ )
457 if( anIt.key()->isToggleAction() && hasRule( anIt.key(), false ) )
458 anIt.key()->setOn( isSatisfied( anIt.key(), false ) );
460 setWidget( ( QWidget* )p );
464 //================================================================
467 //================================================================
468 QtxPopupMgr::RulesMap& QtxPopupMgr::map( bool visibility ) const
470 return ( RulesMap& )( visibility ? myVisibility : myToggle );
473 //================================================================
476 //================================================================
477 bool QtxPopupMgr::load( const QString& fname, QtxActionMgr::Reader& r )
479 PopupCreator cr( &r, this );
480 return r.read( fname, cr );
486 //================================================================
489 //================================================================
490 QtxPopupMgr::PopupCreator::PopupCreator( QtxActionMgr::Reader* r,
492 : QtxActionMgr::Creator( r ),
497 //================================================================
500 //================================================================
501 QtxPopupMgr::PopupCreator::~PopupCreator()
505 //================================================================
508 //================================================================
509 int QtxPopupMgr::PopupCreator::append( const QString& tag, const bool subMenu,
510 const ItemAttributes& attr, const int pId )
512 if( !myMgr || !reader() )
515 QString label = reader()->option( "label", "label" ),
516 id = reader()->option( "id", "id" ),
517 pos = reader()->option( "pos", "pos" ),
518 group = reader()->option( "group", "group" ),
519 tooltip = reader()->option( "tooltip", "tooltip" ),
520 sep = reader()->option( "separator", "separator" ),
521 accel = reader()->option( "accel", "accel" ),
522 icon = reader()->option( "icon", "icon" ),
523 toggle = reader()->option( "toggle", "toggle" );
525 int res = -1, actId = intValue( attr, id, -1 );;
527 res = myMgr->insert( strValue( attr, label ), pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
529 res = myMgr->insert( separator(), pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
530 else //if( !myMgr->contains( actId ) )
532 QPixmap pix; QIconSet set;
533 QString name = strValue( attr, icon );
534 if( !name.isEmpty() )
536 if( loadPixmap( name, pix ) )
537 set = QIconSet( pix );
540 QString actLabel = strValue( attr, label );
541 QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set, actLabel,
542 QKeySequence( strValue( attr, accel ) ),
544 newAct->setToolTip( strValue( attr, tooltip ) );
545 QString toggleact = strValue( attr, toggle );
546 bool isToggle = !toggleact.isEmpty();
547 newAct->setToggleAction( isToggle );
548 newAct->setOn( toggleact.lower()=="true" );
551 int aid = myMgr->registerAction( newAct, visibleRule( attr ),
552 isToggle ? toggleRule( attr ) : QString::null,
554 res = myMgr->insert( aid, pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
560 //================================================================
563 //================================================================
564 QString QtxPopupMgr::PopupCreator::visibleRule( const ItemAttributes& ) const
566 return QString::null;
569 //================================================================
572 //================================================================
573 QString QtxPopupMgr::PopupCreator::toggleRule( const ItemAttributes& ) const
575 return QString::null;