1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxMRUAction.cxx
23 // Author: Sergey TELKOV
25 #include "QtxMRUAction.h"
27 #include "QtxResourceMgr.h"
34 \brief Menu action which provides most recent used items support.
39 \param parent parent object
41 QtxMRUAction::QtxMRUAction( QObject* parent )
42 : QtxAction( tr( "Most Recently Used" ), tr( "Most Recently Used" ), 0, parent ),
45 myLinkType( LinkAuto ),
46 myInsertMode( MoveFirst )
48 myClear = new QAction( tr( "Clear" ), this );
49 myClear->setVisible( false );
51 setMenu( new QMenu( 0 ) );
53 connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
54 connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
59 \param description (tooltip) text
60 \param menuText menu text
61 \param parent parent object
63 QtxMRUAction::QtxMRUAction( const QString& text, const QString& menuText, QObject* parent )
64 : QtxAction( text, menuText, 0, parent ),
67 myLinkType( LinkAuto ),
68 myInsertMode( MoveFirst )
70 myClear = new QAction( tr( "Clear" ), this );
71 myClear->setVisible( false );
73 setMenu( new QMenu( 0 ) );
74 connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
75 connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
80 \param description (tooltip) text
81 \param icon action icon
82 \param menuText menu text
83 \param parent parent object
85 QtxMRUAction::QtxMRUAction( const QString& text, const QIcon& icon,
86 const QString& menuText, QObject* parent )
87 : QtxAction( text, icon, menuText, 0, parent ),
90 myLinkType( LinkAuto ),
91 myInsertMode( MoveFirst )
93 myClear = new QAction( tr( "Clear" ), this );
94 myClear->setVisible( false );
96 setMenu( new QMenu( 0 ) );
97 connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
98 connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
104 QtxMRUAction::~QtxMRUAction()
110 \brief Get items insertion policy.
111 \return insertion policy (QtxMRUAction::InsertionMode)
113 int QtxMRUAction::insertMode() const
119 \brief Set items insertion policy.
120 \param mode insertion policy (QtxMRUAction::InsertionMode)
122 void QtxMRUAction::setInsertMode( const int mode )
128 \brief Get the type of link menu name.
129 \return link type (QtxMRUAction::LinkType)
131 int QtxMRUAction::linkType() const
137 \brief Set the type of link menu name.
138 \param link type (QtxMRUAction::LinkType)
140 void QtxMRUAction::setLinkType( const int type )
146 \brief Get number of MRU items.
147 \return number of MRU items
149 int QtxMRUAction::count() const
151 return myLinks.count();
155 \brief Check if the MRU items list is empty.
156 \return \c true if there are no MRU items
158 bool QtxMRUAction::isEmpty() const
160 return myLinks.isEmpty();
164 \brief Get number of visible MRU items.
165 \return visible MRU items number
166 \sa setVisibleCount()
168 int QtxMRUAction::visibleCount() const
174 \brief Set number of visible MRU items.
176 This method sets the maximum number of MRU items
177 to be displayed in the popup menu (5 by default).
179 If \a num < 1, then all MRU items will be displayed.
181 \param num visible MRU items number
183 void QtxMRUAction::setVisibleCount( int num )
185 if ( myVisCount == num )
192 \brief Return visible status of the menu item which clear all MRU items.
194 bool QtxMRUAction::isClearPossible() const
196 return myClear->isVisible();
200 \brief Set visible the menu item which clear all MRU items.
202 void QtxMRUAction::setClearPossible( const bool on )
204 myClear->setVisible( on );
208 \brief Get number of totally stored MRU items.
209 \return number of MRU items stored in the preferences
210 \sa setHistoryCount(), saveLinks(), loadLinks()
212 int QtxMRUAction::historyCount() const
214 return myHistoryCount;
218 \brief Set number of totally stored MRU items.
220 This option allows setting number of MRU items to be stored
221 in the preferences file.
223 If \a num < 0, then number of stored MRU items is not limited.
225 \return number of MRU items stored in the preferences
226 \sa historyCount(), saveLinks(), loadLinks()
228 void QtxMRUAction::setHistoryCount( const int num )
230 myHistoryCount = num;
234 \brief Insert MRU item.
236 The item is inserted according to the current insertion policy.
238 \param link MRU item to be added
240 void QtxMRUAction::insert( const QString& link )
242 if ( myLinks.contains( link ) && ( insertMode() == AddFirst || insertMode() == AddLast ) )
245 myLinks.removeAll( link );
247 switch ( insertMode() )
251 myLinks.prepend( link );
255 myLinks.append( link );
261 \brief Remove MRU item.
263 Does nothing if \a idx is out of range.
265 \param idx MRU item index
267 void QtxMRUAction::remove( const int idx )
269 if ( idx < 0 || idx >= (int)myLinks.count() )
272 myLinks.removeAt( idx );
276 \brief Remove MRU item.
278 Does nothing if there is no speicified item in the list.
280 \param link MRU item to be removed
282 void QtxMRUAction::remove( const QString& link )
284 myLinks.removeAll( link );
288 \brief Remove all MRU items.
290 void QtxMRUAction::clear()
297 \param idx MRU item index
298 \return MRU item or null QString if \a idx is out of range
300 QString QtxMRUAction::item( const int idx ) const
303 if ( idx >= 0 && idx < (int)myLinks.count() )
309 \brief Get MRU item index.
311 \return MRU item index or -1 if item is not found
313 int QtxMRUAction::find( const QString& link ) const
315 return myLinks.indexOf( link );
319 \brief Check if MRU item is in the list.
321 \return \c true if specified item is already added to the list
323 bool QtxMRUAction::contains( const QString& link ) const
325 return myLinks.contains( link );
329 \brief Load the MRU items from specified resources section.
330 \param resMgr resources manager
331 \param section resources section
332 \param clear if \c true, previous MRU items list is cleared
334 void QtxMRUAction::loadLinks( QtxResourceMgr* resMgr, const QString& section, const bool clear )
336 if ( !resMgr || section.isEmpty() )
342 QString itemPrefix( "item_" );
344 QMap<QString, int> map;
345 for ( QStringList::const_iterator itr = myLinks.begin(); itr != myLinks.end(); ++ itr )
346 map.insert( *itr, 0 );
348 QStringList items = resMgr->parameters( section );
349 for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
351 if ( !(*it).startsWith( itemPrefix ) )
354 QString link = resMgr->stringValue( section, *it, QString() );
355 if ( link.isEmpty() || map.contains( link ) )
358 myLinks.append( link );
359 map.insert( link, 0 );
364 \brief Save the MRU items to specified resources section.
365 \param resMgr resources manager
366 \param section resources section
367 \param clear if \c true, the resources section is first cleared
369 void QtxMRUAction::saveLinks( QtxResourceMgr* resMgr, const QString& section, const bool clear ) const
371 if ( !resMgr || section.isEmpty() )
374 QString itemPrefix( "item_" );
377 QStringList items = resMgr->parameters( section );
378 for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
380 if ( (*it).startsWith( itemPrefix ) )
381 resMgr->remove( section, *it );
386 QMap<QString, int> map;
387 for ( QStringList::const_iterator itr = myLinks.begin(); itr != myLinks.end(); ++itr )
390 map.insert( *itr, 0 );
393 QStringList items = resMgr->parameters( section );
394 for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
396 if ( !(*it).startsWith( itemPrefix ) )
399 QString link = resMgr->stringValue( section, *it, QString() );
400 if ( !link.isEmpty() && !map.contains( link ) )
403 map.insert( link, 0 );
406 resMgr->remove( section, *it );
410 for ( QStringList::const_iterator iter = lst.begin();
411 iter != lst.end() && ( myHistoryCount < 0 || counter < myHistoryCount );
413 resMgr->setValue( section, itemPrefix + QString().sprintf( "%03d", counter ), *iter );
417 \brief Prepare MRU items popup menu.
419 This method is called when the parent menu is shown.
420 Enables or disables sub menu item according to the number of MRU items.
422 void QtxMRUAction::onAboutToShow()
428 \brief Called when any MRU item is selected by the user.
430 Emits signal activated(const QString&) passing selected MRU item as parameter.
432 void QtxMRUAction::onActivated()
434 QAction* a = ::qobject_cast<QAction*>( sender() );
438 QString link = a->data().toString();
439 if ( !link.isEmpty() && myLinks.contains( link ) )
440 emit activated( link );
443 void QtxMRUAction::onCleared( bool )
449 \brief Update MRU items popup menu.
451 void QtxMRUAction::updateMenu()
460 QMap<QString, int> map;
461 int count = visibleCount() < 0 ? myLinks.count() : visibleCount();
462 int i = insertMode() == AddLast || insertMode() == MoveLast ? qMax( 0, myLinks.count()-count ) : 0;
463 for ( ; i < myLinks.count() && count > 0; ++i, count-- )
465 links.append( myLinks[i] );
466 if ( linkType() == LinkAuto )
468 QString shortName = Qtx::file( myLinks[i] );
469 if ( map.contains( shortName ) )
472 map.insert( shortName, 0 );
477 for ( QStringList::const_iterator it = links.begin(); it != links.end(); ++it, i++ )
483 linkName = Qtx::file( *it );
484 if ( map.contains( linkName ) && map[linkName] )
488 linkName = Qtx::file( *it );
496 if ( links.count() < 10 )
497 linkName = QString( "&%1 %2" ).arg( i ).arg( linkName );
499 pm->addAction( linkName, this, SLOT( onActivated() ) )->setData( *it );
503 pm->addAction( tr( "<Empty>" ) )->setEnabled( false );
505 if ( isClearPossible() )
508 pm->addAction( myClear );
509 myClear->setEnabled( !pm->isEmpty() );
514 \fn void QtxMRUAction::activated( const QString& link );
515 \brief Emitted when user selects any MRU item in the menu.
516 \param link selected MRU item