2 #include "QtxPopupMgr.h"
3 #include "QtxListOfOperations.h"
4 #include "QtxStdOperations.h"
7 #include <qpopupmenu.h>
9 //================================================================
12 //================================================================
13 QtxValue QtxPopupMgr::Selection::globalParam( const QString& str ) const
15 if( str==selCountParam() )
18 else if( str[0]==equality() )
20 QtxSets::ValueSet set;
21 QString par = str.mid( 1 );
23 for( int i=0, n=count(); i<n; i++ )
25 QtxValue v = param( i, par );
27 QtxSets::add( set, v );
38 //================================================================
41 //================================================================
42 QChar QtxPopupMgr::Selection::equality() const
47 //================================================================
50 //================================================================
51 QString QtxPopupMgr::Selection::selCountParam() const
53 return defSelCountParam();
56 //================================================================
59 //================================================================
60 QChar QtxPopupMgr::Selection::defEquality()
65 //================================================================
68 //================================================================
69 QString QtxPopupMgr::Selection::defSelCountParam()
77 //================================================================
80 //================================================================
81 QtxPopupMgr::Operations::Operations( QtxPopupMgr* mgr )
86 aList.append( "every" );
87 aList.append( "any" );
88 aList.append( "onlyone" );
89 addOperations( aList );
91 myParser = new QtxParser( mgr->myOperations );
94 //================================================================
97 //================================================================
98 QtxPopupMgr::Operations::~Operations()
103 //================================================================
106 //================================================================
107 int QtxPopupMgr::Operations::prior( const QString& op, bool isBin ) const
109 if( !isBin && ( op=="every" || op=="any" || op=="onlyone" ) )
112 return QtxStrings::prior( op, isBin );
116 //================================================================
119 //================================================================
120 QtxParser::Error QtxPopupMgr::Operations::calculate
121 ( const QString& op, QtxValue& v1, QtxValue& v2 ) const
128 else if( op=="onlyone" )
131 if( ind>=0 && ind<=2 )
133 QString val_name = op + "(" + v2.toString() + ")";
134 QtxParser::Error err = QtxParser::OK;
136 if( !myValues.contains( val_name ) )
138 myParser->setExpr( v2.toString() );
139 QStringList params, specific;
140 myParser->paramsList( params );
143 myPopupMgr->setParams( myParser, specific );
145 QtxPopupMgr::Selection* sel = myPopupMgr->myCurrentSelection;
147 int global_result = 0;
149 for( int i=0, n=sel->count(); i<n; i++ )
151 QStringList::const_iterator anIt = specific.begin(),
152 aLast = specific.end();
153 for( ; anIt!=aLast; anIt++ )
155 QtxValue v = sel->param( i, *anIt );
157 myParser->set( *anIt, v );
159 return QtxParser::InvalidToken;
162 QtxValue res = myParser->calculate();
163 err = myParser->lastError();
164 if( err==QtxParser::OK )
165 if( res.type()==QVariant::Bool )
169 if( ind==2 && global_result>1 )
173 return QtxParser::InvalidResult;
178 QtxValue& vv = ( QtxValue& )myValues[ val_name ];
179 vv = ( ind==0 && global_result==sel->count() ) ||
181 ( ind==2 && global_result==1 );
184 v2 = myValues[ val_name ];
189 return QtxStrings::calculate( op, v1, v2 );
192 //================================================================
195 //================================================================
196 void QtxPopupMgr::Operations::clear()
209 //================================================================
212 //================================================================
213 QtxPopupMgr::QtxPopupMgr( QPopupMenu* popup, QObject* parent )
214 : QtxActionMenuMgr( popup, parent ),
215 myCurrentSelection( 0 )
220 //================================================================
223 //================================================================
224 QtxPopupMgr::~QtxPopupMgr()
228 //================================================================
231 //================================================================
232 void QtxPopupMgr::createOperations()
234 myOperations = new QtxListOfOperations;
235 myOperations->append( "logic", new QtxLogic(), 0 );
236 myOperations->append( "arithm", new QtxArithmetics(), 50 );
237 myOperations->append( "strings", new QtxStrings(), 100 );
238 myOperations->append( "sets", new QtxSets(), 150 );
239 myOperations->append( "custom", new Operations( this ), 200 );
242 //================================================================
245 //================================================================
246 int QtxPopupMgr::registerAction( QAction* act,
247 const QString& visible,
248 const QString& toggle,
251 int _id = QtxActionMenuMgr::registerAction( act, id );
252 setRule( _id, visible, true );
253 setRule( _id, toggle, false );
257 //================================================================
260 //================================================================
261 void QtxPopupMgr::unRegisterAction( const int id )
263 QAction* act = action( id );
265 myVisibility.remove( act );
266 myToggle.remove( act );
269 //QtxActionMenuMgr::unRegisterAction( id );
272 //================================================================
275 //================================================================
276 bool QtxPopupMgr::hasRule( QAction* act, bool visibility ) const
278 return map( visibility ).contains( act );
281 //================================================================
284 //================================================================
285 bool QtxPopupMgr::hasRule( const int id, bool visibility ) const
287 return hasRule( action( id ), visibility );
290 //================================================================
293 //================================================================
294 void QtxPopupMgr::setRule( QAction* act, const QString& rule, bool visibility )
296 if( !act || rule.isEmpty() )
299 if( !hasRule( act, visibility ) )
301 QtxParser* p = new QtxParser( myOperations, rule );
302 if( p->lastError()==QtxParser::OK )
303 map( visibility ).insert( act, p );
309 QtxParser* p = map( visibility )[ act ];
311 if( p->lastError()!=QtxParser::OK )
312 p->setExpr( QString() );
316 //================================================================
319 //================================================================
320 void QtxPopupMgr::setRule( const int id, const QString& rule, bool visibility )
322 setRule( action( id ), rule, visibility );
325 //================================================================
328 //================================================================
329 bool result( QtxParser* p )
334 QtxValue vv = p->calculate();
335 res = p->lastError()==QtxParser::OK &&
336 ( ( vv.type()==QVariant::Int && vv.toInt()!=0 ) ||
337 ( vv.type()==QVariant::Bool && vv.toBool() ) );
342 //================================================================
345 //================================================================
346 void QtxPopupMgr::setParams( QtxParser* p, QStringList& specific ) const
348 if( !p || !myCurrentSelection )
353 p->paramsList( params );
354 QStringList::const_iterator anIt = params.begin(),
355 aLast = params.end();
356 for( ; anIt!=aLast; anIt++ )
358 QtxValue v = myCurrentSelection->globalParam( *anIt );
362 specific.append( *anIt );
366 //================================================================
369 //================================================================
370 bool QtxPopupMgr::isSatisfied( QAction* act, bool visibility ) const
372 QString menu = act->menuText();
373 //const char* mmm = menu.latin1();
379 if( hasRule( act, visibility ) )
381 QtxParser* p = map( visibility )[ act ];
382 //QString pdump = p->dump();
383 //const char* pdd = pdump.latin1();
386 QStringList specific;
388 ( ( Operations* )myOperations->operations( "custom" ) )->clear();
390 setParams( p, specific );
392 if( specific.count()>0 )
393 if( myCurrentSelection )
394 for( int i=0, n=myCurrentSelection->count(); i<n; i++ )
396 QStringList::const_iterator anIt1 = specific.begin(),
397 aLast1 = specific.end();
398 for( ; anIt1!=aLast1; anIt1++ )
399 p->set( *anIt1, myCurrentSelection->param( i, *anIt1 ) );
401 res = res || result( p );
412 //================================================================
415 //================================================================
416 bool QtxPopupMgr::isVisible( const int actId, const int place ) const
418 bool res = QtxActionMenuMgr::isVisible( actId, place );
419 QAction* act = action( actId );
420 if( hasRule( act, true ) )
421 res = res && isSatisfied( act, true );
425 //================================================================
428 //================================================================
429 void QtxPopupMgr::updatePopup( QPopupMenu* p, Selection* sel )
434 myCurrentSelection = sel;
436 RulesMap::iterator anIt = myToggle.begin(),
437 aLast = myToggle.end();
438 for( ; anIt!=aLast; anIt++ )
439 if( anIt.key()->isToggleAction() && hasRule( anIt.key(), false ) )
440 anIt.key()->setOn( isSatisfied( anIt.key(), false ) );
442 setWidget( ( QWidget* )p );
446 //================================================================
449 //================================================================
450 QtxPopupMgr::RulesMap& QtxPopupMgr::map( bool visibility ) const
452 return ( RulesMap& )( visibility ? myVisibility : myToggle );
455 //================================================================
458 //================================================================
459 bool QtxPopupMgr::load( const QString& fname, QtxActionMgr::Reader& r )
461 PopupCreator cr( &r, this );
462 return r.read( fname, cr );
468 //================================================================
471 //================================================================
472 QtxPopupMgr::PopupCreator::PopupCreator( QtxActionMgr::Reader* r,
474 : QtxActionMgr::Creator( r ),
479 //================================================================
482 //================================================================
483 QtxPopupMgr::PopupCreator::~PopupCreator()
487 //================================================================
490 //================================================================
491 int QtxPopupMgr::PopupCreator::append( const QString& tag, const bool subMenu,
492 const ItemAttributes& attr, const int pId )
494 if( !myMgr || !reader() )
497 QString label = reader()->option( "label", "label" ),
498 id = reader()->option( "id", "id" ),
499 pos = reader()->option( "pos", "pos" ),
500 group = reader()->option( "group", "group" ),
501 tooltip = reader()->option( "tooltip", "tooltip" ),
502 sep = reader()->option( "separator", "separator" ),
503 accel = reader()->option( "accel", "accel" ),
504 icon = reader()->option( "icon", "icon" ),
505 toggle = reader()->option( "toggle", "toggle" );
507 int res = -1, actId = intValue( attr, id, -1 );;
509 res = myMgr->insert( strValue( attr, label ), pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
511 res = myMgr->insert( separator(), pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
512 else //if( !myMgr->contains( actId ) )
514 QPixmap pix; QIconSet set;
515 QString name = strValue( attr, icon );
516 if( !name.isEmpty() )
518 if( loadPixmap( name, pix ) )
519 set = QIconSet( pix );
522 QString actLabel = strValue( attr, label );
523 QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set, actLabel,
524 QKeySequence( strValue( attr, accel ) ),
526 newAct->setToolTip( strValue( attr, tooltip ) );
527 QString toggleact = strValue( attr, toggle );
528 bool isToggle = !toggleact.isEmpty();
529 newAct->setToggleAction( isToggle );
530 newAct->setOn( toggleact.lower()=="true" );
533 int aid = myMgr->registerAction( newAct, visibleRule( attr ),
534 isToggle ? toggleRule( attr ) : QString::null,
536 res = myMgr->insert( aid, pId, intValue( attr, group, 0 ), intValue( attr, pos, -1 ) );
542 //================================================================
545 //================================================================
546 QString QtxPopupMgr::PopupCreator::visibleRule( const ItemAttributes& ) const
548 return QString::null;
551 //================================================================
554 //================================================================
555 QString QtxPopupMgr::PopupCreator::toggleRule( const ItemAttributes& ) const
557 return QString::null;