2 #include "QtxPopupMgr.h"
3 #include "QtxListOfOperations.h"
4 #include "QtxStdOperations.h"
7 #include <qpopupmenu.h>
9 //================================================================
12 //================================================================
13 QtxPopupMgr::Operations::Operations( QtxPopupMgr* mgr )
18 aList.append( "every" );
19 aList.append( "any" );
20 aList.append( "onlyone" );
21 addOperations( aList );
23 myParser = new QtxParser( mgr->myOperations );
26 //================================================================
29 //================================================================
30 QtxPopupMgr::Operations::~Operations()
35 //================================================================
38 //================================================================
39 int QtxPopupMgr::Operations::prior( const QString& op, bool isBin ) const
41 if( !isBin && ( op=="every" || op=="any" || op=="onlyone" ) )
44 return QtxStrings::prior( op, isBin );
48 //================================================================
51 //================================================================
52 QtxParser::Error QtxPopupMgr::Operations::calculate
53 ( const QString& op, QtxValue& v1, QtxValue& v2 ) const
60 else if( op=="onlyone" )
63 if( ind>=0 && ind<=2 )
65 QString val_name = op + "(" + v2.toString() + ")";
66 QtxParser::Error err = QtxParser::OK;
68 if( !myValues.contains( val_name ) )
70 myParser->setExpr( v2.toString() );
71 QStringList params, specific;
72 myParser->paramsList( params );
75 myPopupMgr->setParams( myParser, specific );
77 QtxPopupMgr::Selection* sel = myPopupMgr->myCurrentSelection;
79 int global_result = 0;
81 for( int i=0, n=sel->count(); i<n; i++ )
83 QStringList::const_iterator anIt = specific.begin(),
84 aLast = specific.end();
85 for( ; anIt!=aLast; anIt++ )
87 QtxValue v = sel->param( i, *anIt );
89 myParser->set( *anIt, v );
91 return QtxParser::InvalidToken;
94 QtxValue res = myParser->calculate();
95 err = myParser->lastError();
96 if( err==QtxParser::OK )
97 if( res.type()==QVariant::Bool )
101 if( ind==2 && global_result>1 )
105 return QtxParser::InvalidResult;
110 QtxValue& vv = ( QtxValue& )myValues[ val_name ];
111 vv = ( ind==0 && global_result==sel->count() ) ||
113 ( ind==2 && global_result==1 );
116 v2 = myValues[ val_name ];
121 return QtxStrings::calculate( op, v1, v2 );
124 //================================================================
127 //================================================================
128 void QtxPopupMgr::Operations::clear()
141 //================================================================
144 //================================================================
145 QtxPopupMgr::QtxPopupMgr( QPopupMenu* popup, QObject* parent )
146 : QtxActionMenuMgr( popup, parent )
151 //================================================================
154 //================================================================
155 QtxPopupMgr::~QtxPopupMgr()
159 //================================================================
162 //================================================================
163 void QtxPopupMgr::createOperations()
165 myOperations = new QtxListOfOperations;
166 myOperations->append( "logic", new QtxLogic(), 0 );
167 myOperations->append( "arithm", new QtxArithmetics(), 50 );
168 myOperations->append( "strings", new QtxStrings(), 100 );
169 myOperations->append( "sets", new QtxSets(), 150 );
170 myOperations->append( "custom", new Operations( this ), 200 );
173 //================================================================
176 //================================================================
177 int QtxPopupMgr::registerAction( QAction* act,
178 const QString& visible,
179 const QString& toggle,
182 int _id = QtxActionMenuMgr::registerAction( act, id );
183 setRule( _id, visible, true );
184 setRule( _id, toggle, false );
188 //================================================================
191 //================================================================
192 void QtxPopupMgr::unRegisterAction( const int id )
194 QAction* act = action( id );
196 myVisibility.remove( act );
197 myToggle.remove( act );
200 //QtxActionMenuMgr::unRegisterAction( id );
203 //================================================================
206 //================================================================
207 bool QtxPopupMgr::hasRule( QAction* act, bool visibility ) const
209 return map( visibility ).contains( act );
212 //================================================================
215 //================================================================
216 bool QtxPopupMgr::hasRule( const int id, bool visibility ) const
218 return hasRule( action( id ), visibility );
221 //================================================================
224 //================================================================
225 void QtxPopupMgr::setRule( QAction* act, const QString& rule, bool visibility )
227 if( !act || rule.isEmpty() )
230 if( !hasRule( act, visibility ) )
232 QtxParser* p = new QtxParser( myOperations, rule );
233 if( p->lastError()==QtxParser::OK )
234 map( visibility ).insert( act, p );
240 QtxParser* p = map( visibility )[ act ];
242 if( p->lastError()!=QtxParser::OK )
243 p->setExpr( QString() );
247 //================================================================
250 //================================================================
251 void QtxPopupMgr::setRule( const int id, const QString& rule, bool visibility )
253 setRule( action( id ), rule, visibility );
256 //================================================================
259 //================================================================
260 bool result( QtxParser* p )
265 QtxValue vv = p->calculate();
266 res = p->lastError()==QtxParser::OK &&
267 ( ( vv.type()==QVariant::Int && vv.toInt()!=0 ) ||
268 ( vv.type()==QVariant::Bool && vv.toBool() ) );
273 //================================================================
276 //================================================================
277 void QtxPopupMgr::setParams( QtxParser* p, QStringList& specific ) const
284 p->paramsList( params );
285 QStringList::const_iterator anIt = params.begin(),
286 aLast = params.end();
287 for( ; anIt!=aLast; anIt++ )
289 if( *anIt==selCountParam() )
291 if( myCurrentSelection )
292 p->set( *anIt, myCurrentSelection->count() );
297 else if( (*anIt)[0]==equality() )
299 QtxSets::ValueSet set;
300 QString par = ( *anIt ).mid( 1 );
302 if( myCurrentSelection && myCurrentSelection->count() > 0 )
303 for( int i=0, n=myCurrentSelection->count(); i<n; i++ )
304 QtxSets::add( set, myCurrentSelection->param( i, par ) );
306 p->set( *anIt, set );
309 specific.append( *anIt );
312 //================================================================
315 //================================================================
316 bool QtxPopupMgr::isSatisfied( QAction* act, bool visibility ) const
318 QString menu = act->menuText();
319 //const char* mmm = menu.latin1();
325 if( hasRule( act, visibility ) )
327 QtxParser* p = map( visibility )[ act ];
328 //QString pdump = p->dump();
329 //const char* pdd = pdump.latin1();
332 QStringList specific;
334 ( ( Operations* )myOperations->operations( "custom" ) )->clear();
336 setParams( p, specific );
338 if( specific.count()>0 )
339 if( myCurrentSelection )
340 for( int i=0, n=myCurrentSelection->count(); i<n; i++ )
342 QStringList::const_iterator anIt1 = specific.begin(),
343 aLast1 = specific.end();
344 for( ; anIt1!=aLast1; anIt1++ )
345 p->set( *anIt1, myCurrentSelection->param( i, *anIt1 ) );
347 res = res || result( p );
358 //================================================================
361 //================================================================
362 bool QtxPopupMgr::isVisible( const int actId, const int place ) const
364 bool res = QtxActionMenuMgr::isVisible( actId, place );
365 QAction* act = action( actId );
366 if( hasRule( act, true ) )
367 res = res && isSatisfied( act, true );
371 //================================================================
374 //================================================================
375 void QtxPopupMgr::updatePopup( QPopupMenu* p, Selection* sel )
380 myCurrentSelection = sel;
382 RulesMap::iterator anIt = myToggle.begin(),
383 aLast = myToggle.end();
384 for( ; anIt!=aLast; anIt++ )
385 if( anIt.key()->isToggleAction() && hasRule( anIt.key(), false ) )
386 anIt.key()->setOn( isSatisfied( anIt.key(), false ) );
388 setWidget( ( QWidget* )p );
392 //================================================================
395 //================================================================
396 QtxPopupMgr::RulesMap& QtxPopupMgr::map( bool visibility ) const
398 return ( RulesMap& )( visibility ? myVisibility : myToggle );
401 //================================================================
404 //================================================================
405 QString QtxPopupMgr::selCountParam() const
410 //================================================================
413 //================================================================
414 QChar QtxPopupMgr::equality() const
419 //================================================================
422 //================================================================
423 bool QtxPopupMgr::load( const QString& fname, QtxActionMgr::Reader& r )
425 PopupCreator cr( &r, this );
426 return r.read( fname, cr );
432 //================================================================
435 //================================================================
436 QtxPopupMgr::PopupCreator::PopupCreator( QtxActionMgr::Reader* r,
438 : QtxActionMgr::Creator( r ),
443 //================================================================
446 //================================================================
447 QtxPopupMgr::PopupCreator::~PopupCreator()
451 //================================================================
454 //================================================================
455 int QtxPopupMgr::PopupCreator::append( const QString& tag, const bool subMenu,
456 const ItemAttributes& attr, const int pId )
458 if( !myMgr || !reader() )
461 QString label = reader()->option( "label", "label" ),
462 id = reader()->option( "id", "id" ),
463 pos = reader()->option( "pos", "pos" ),
464 group = reader()->option( "group", "group" ),
465 tooltip = reader()->option( "tooltip", "tooltip" ),
466 sep = reader()->option( "separator", "separator" ),
467 accel = reader()->option( "accel", "accel" ),
468 icon = reader()->option( "icon", "icon" ),
469 toggle = reader()->option( "toggle", "toggle" );
471 int res = -1, actId = intValue( attr, id, -1 );;
473 res = myMgr->insert( strValue( attr, label ), pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
475 res = myMgr->insert( separator(), pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
476 else //if( !myMgr->contains( actId ) )
478 QPixmap pix; QIconSet set;
479 QString name = strValue( attr, icon );
480 if( !name.isEmpty() )
482 if( loadPixmap( name, pix ) )
483 set = QIconSet( pix );
486 QString actLabel = strValue( attr, label );
487 QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set, actLabel,
488 QKeySequence( strValue( attr, accel ) ),
490 newAct->setToolTip( strValue( attr, tooltip ) );
491 QString toggleact = strValue( attr, toggle );
492 bool isToggle = !toggleact.isEmpty();
493 newAct->setToggleAction( isToggle );
494 newAct->setOn( toggleact.lower()=="true" );
497 int aid = myMgr->registerAction( newAct, visibleRule( attr ),
498 isToggle ? toggleRule( attr ) : QString::null,
500 res = myMgr->insert( aid, pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
506 //================================================================
509 //================================================================
510 QString QtxPopupMgr::PopupCreator::visibleRule( const ItemAttributes& ) const
512 return QString::null;
515 //================================================================
518 //================================================================
519 QString QtxPopupMgr::PopupCreator::toggleRule( const ItemAttributes& ) const
521 return QString::null;