Back to index

salome-gui  6.5.0
QtxMRUAction.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxMRUAction.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxMRUAction.h"
00027 
00028 #include "QtxResourceMgr.h"
00029 
00030 #include <QMenu>
00031 #include <QIcon>
00032 
00042 QtxMRUAction::QtxMRUAction( QObject* parent )
00043 : QtxAction( tr( "Most Recently Used" ), tr( "Most Recently Used" ), 0, parent ),
00044   myVisCount( 5 ),
00045   myHistoryCount( -1 ),
00046   myLinkType( LinkAuto ),
00047   myInsertMode( MoveFirst )
00048 {
00049   myClear = new QAction( tr( "Clear" ), this );
00050   myClear->setVisible( false );
00051 
00052   setMenu( new QMenu( 0 ) );
00053 
00054   connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00055   connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
00056 }
00057 
00064 QtxMRUAction::QtxMRUAction( const QString& text, const QString& menuText, QObject* parent )
00065 : QtxAction( text, menuText, 0, parent ),
00066   myVisCount( 5 ),
00067   myHistoryCount( -1 ),
00068   myLinkType( LinkAuto ),
00069   myInsertMode( MoveFirst )
00070 {
00071   myClear = new QAction( tr( "Clear" ), this );
00072   myClear->setVisible( false );
00073 
00074   setMenu( new QMenu( 0 ) );
00075   connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00076   connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
00077 }
00078 
00086 QtxMRUAction::QtxMRUAction( const QString& text, const QIcon& icon,
00087                             const QString& menuText, QObject* parent )
00088 : QtxAction( text, icon, menuText, 0, parent ),
00089   myVisCount( 5 ),
00090   myHistoryCount( -1 ),
00091   myLinkType( LinkAuto ),
00092   myInsertMode( MoveFirst )
00093 {
00094   myClear = new QAction( tr( "Clear" ), this );
00095   myClear->setVisible( false );
00096 
00097   setMenu( new QMenu( 0 ) );
00098   connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00099   connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
00100 }
00101 
00105 QtxMRUAction::~QtxMRUAction()
00106 {
00107   delete menu();
00108 }
00109 
00114 int QtxMRUAction::insertMode() const
00115 {
00116   return myInsertMode;
00117 }
00118 
00123 void QtxMRUAction::setInsertMode( const int mode )
00124 {
00125   myInsertMode = mode;
00126 }
00127 
00132 int QtxMRUAction::linkType() const
00133 {
00134   return myLinkType;
00135 }
00136 
00141 void QtxMRUAction::setLinkType( const int type )
00142 {
00143   myLinkType = type;
00144 }
00145 
00150 int QtxMRUAction::count() const
00151 {
00152   return myLinks.count();
00153 }
00154 
00159 bool QtxMRUAction::isEmpty() const
00160 {
00161   return myLinks.isEmpty();
00162 }
00163 
00169 int QtxMRUAction::visibleCount() const
00170 {
00171   return myVisCount;
00172 }
00173 
00184 void QtxMRUAction::setVisibleCount( int num )
00185 {
00186   if ( myVisCount == num )
00187     return;
00188 
00189   myVisCount = num;
00190 }
00191 
00195 bool QtxMRUAction::isClearPossible() const
00196 {
00197   return myClear->isVisible();
00198 }
00199 
00203 void QtxMRUAction::setClearPossible( const bool on )
00204 {
00205   myClear->setVisible( on );
00206 }
00207 
00213 int QtxMRUAction::historyCount() const
00214 {
00215   return myHistoryCount;
00216 }
00217 
00229 void QtxMRUAction::setHistoryCount( const int num )
00230 {
00231   myHistoryCount = num;
00232 }
00233 
00241 void QtxMRUAction::insert( const QString& link )
00242 {
00243   if ( myLinks.contains( link ) && ( insertMode() == AddFirst || insertMode() == AddLast ) )
00244     return;
00245 
00246   myLinks.removeAll( link );
00247 
00248   switch ( insertMode() )
00249   {
00250   case AddFirst:
00251   case MoveFirst:
00252     myLinks.prepend( link );
00253     break;
00254   case AddLast:
00255   case MoveLast:
00256     myLinks.append( link );
00257     break;
00258   }
00259 }
00260 
00268 void QtxMRUAction::remove( const int idx )
00269 {
00270   if ( idx < 0 || idx >= (int)myLinks.count() )
00271     return;
00272 
00273   myLinks.removeAt( idx );
00274 }
00275 
00283 void QtxMRUAction::remove( const QString& link )
00284 {
00285   myLinks.removeAll( link );
00286 }
00287 
00291 void QtxMRUAction::clear()
00292 {
00293   myLinks.clear();
00294 }
00295 
00301 QString QtxMRUAction::item( const int idx ) const
00302 {
00303   QString res;
00304   if ( idx >= 0 && idx < (int)myLinks.count() )
00305     res = myLinks[idx];
00306   return res;
00307 }
00308 
00314 int QtxMRUAction::find( const QString& link ) const
00315 {
00316   return myLinks.indexOf( link );
00317 }
00318 
00324 bool QtxMRUAction::contains( const QString& link ) const
00325 {
00326   return myLinks.contains( link );
00327 }
00328 
00335 void QtxMRUAction::loadLinks( QtxResourceMgr* resMgr, const QString& section, const bool clear )
00336 {
00337   if ( !resMgr || section.isEmpty() )
00338     return;
00339 
00340   if ( clear )
00341     myLinks.clear();
00342 
00343   QString itemPrefix( "item_" );
00344 
00345   QMap<QString, int> map;
00346   for ( QStringList::const_iterator itr = myLinks.begin(); itr != myLinks.end(); ++ itr )
00347     map.insert( *itr, 0 );
00348 
00349   QStringList items = resMgr->parameters( section );
00350   for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
00351   {
00352     if ( !(*it).startsWith( itemPrefix ) )
00353       continue;
00354 
00355     QString link = resMgr->stringValue( section, *it, QString() );
00356     if ( link.isEmpty() || map.contains( link ) )
00357       continue;
00358 
00359     myLinks.append( link );
00360     map.insert( link, 0 );
00361   }
00362 }
00363 
00370 void QtxMRUAction::saveLinks( QtxResourceMgr* resMgr, const QString& section, const bool clear ) const
00371 {
00372   if ( !resMgr || section.isEmpty() )
00373     return;
00374 
00375   QString itemPrefix( "item_" );
00376 
00377   if ( clear ) {
00378     QStringList items = resMgr->parameters( section );
00379     for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
00380     {
00381       if ( (*it).startsWith( itemPrefix ) )
00382         resMgr->remove( section, *it );
00383     }
00384   }
00385 
00386   QStringList lst;
00387   QMap<QString, int> map;
00388   for ( QStringList::const_iterator itr = myLinks.begin(); itr != myLinks.end(); ++itr )
00389   {
00390     lst.append( *itr );
00391     map.insert( *itr, 0 );
00392   }
00393 
00394   QStringList items = resMgr->parameters( section );
00395   for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
00396   {
00397     if ( !(*it).startsWith( itemPrefix ) )
00398       continue;
00399 
00400     QString link = resMgr->stringValue( section, *it, QString() );
00401     if ( !link.isEmpty() && !map.contains( link ) )
00402     {
00403       lst.append( link );
00404       map.insert( link, 0 );
00405     }
00406 
00407     resMgr->remove( section, *it );
00408   }
00409 
00410   int counter = 0;
00411   for ( QStringList::const_iterator iter = lst.begin();
00412         iter != lst.end() && ( myHistoryCount < 0 || counter < myHistoryCount );
00413         ++iter, counter++ )
00414     resMgr->setValue( section, itemPrefix + QString().sprintf( "%03d", counter ), *iter );
00415 }
00416 
00423 void QtxMRUAction::onAboutToShow()
00424 {
00425   updateMenu();
00426 }
00427 
00433 void QtxMRUAction::onActivated()
00434 {
00435   QAction* a = ::qobject_cast<QAction*>( sender() );
00436   if ( !a )
00437     return;
00438 
00439   QString link = a->data().toString();
00440   if ( !link.isEmpty() && myLinks.contains( link ) )
00441     emit activated( link );
00442 }
00443 
00444 void QtxMRUAction::onCleared( bool )
00445 {
00446   clear();
00447 }
00448 
00452 void QtxMRUAction::updateMenu()
00453 {
00454   QMenu* pm = menu();
00455   if ( !pm )
00456     return;
00457 
00458   pm->clear();
00459 
00460   QStringList links;
00461   QMap<QString, int> map;
00462   int count = visibleCount() < 0 ? myLinks.count() : visibleCount();
00463   int i = insertMode() == AddLast || insertMode() == MoveLast ? qMax( 0, myLinks.count()-count ) : 0;
00464   for ( ; i < myLinks.count() && count > 0; ++i, count-- )
00465   {
00466     links.append( myLinks[i] );
00467     if ( linkType() == LinkAuto )
00468     {
00469       QString shortName = Qtx::file( myLinks[i] );
00470       if ( map.contains( shortName ) )
00471         map[shortName]++;
00472       else
00473         map.insert( shortName, 0 );
00474     }
00475   }
00476 
00477   i = 1;
00478   for ( QStringList::const_iterator it = links.begin(); it != links.end(); ++it, i++ )
00479   {
00480     QString linkName;
00481     switch( linkType() )
00482     {
00483     case LinkAuto:
00484       linkName = Qtx::file( *it );
00485       if ( map.contains( linkName ) && map[linkName] )
00486         linkName = *it;
00487       break;
00488     case LinkShort:
00489       linkName = Qtx::file( *it );
00490       break;
00491     case LinkFull:
00492     default:
00493       linkName = *it;
00494       break;
00495     }
00496 
00497     if ( links.count() < 10 )
00498       linkName = QString( "&%1 %2" ).arg( i ).arg( linkName );
00499 
00500     pm->addAction( linkName, this, SLOT( onActivated() ) )->setData( *it );
00501   }
00502 
00503   if ( pm->isEmpty() )
00504     pm->addAction( tr( "<Empty>" ) )->setEnabled( false );
00505 
00506   if ( isClearPossible() )
00507   {
00508     pm->addSeparator();
00509     pm->addAction( myClear );
00510     myClear->setEnabled( !pm->isEmpty() );
00511   }
00512 }
00513