1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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
23 // File: QtxMRUAction.cxx
24 // Author: Sergey TELKOV
26 #include "QtxMRUAction.h"
28 #include "QtxResourceMgr.h"
35 \brief Menu action which provides most recent used items support.
40 \param parent parent object
42 QtxMRUAction::QtxMRUAction( QObject* parent )
43 : QtxAction( tr( "Most Recently Used" ), tr( "Most Recently Used" ), 0, parent ),
46 myLinkType( LinkAuto ),
47 myInsertMode( MoveFirst )
49 myClear = new QAction( tr( "Clear" ), this );
50 myClear->setVisible( false );
52 setMenu( new QMenu( 0 ) );
54 connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
55 connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
60 \param description (tooltip) text
61 \param menuText menu text
62 \param parent parent object
64 QtxMRUAction::QtxMRUAction( const QString& text, const QString& menuText, QObject* parent )
65 : QtxAction( text, menuText, 0, parent ),
68 myLinkType( LinkAuto ),
69 myInsertMode( MoveFirst )
71 myClear = new QAction( tr( "Clear" ), this );
72 myClear->setVisible( false );
74 setMenu( new QMenu( 0 ) );
75 connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
76 connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
81 \param description (tooltip) text
82 \param icon action icon
83 \param menuText menu text
84 \param parent parent object
86 QtxMRUAction::QtxMRUAction( const QString& text, const QIcon& icon,
87 const QString& menuText, QObject* parent )
88 : QtxAction( text, icon, menuText, 0, parent ),
91 myLinkType( LinkAuto ),
92 myInsertMode( MoveFirst )
94 myClear = new QAction( tr( "Clear" ), this );
95 myClear->setVisible( false );
97 setMenu( new QMenu( 0 ) );
98 connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
99 connect( myClear, SIGNAL( triggered( bool ) ), this, SLOT( onCleared( bool ) ) );
105 QtxMRUAction::~QtxMRUAction()
111 \brief Get items insertion policy.
112 \return insertion policy (QtxMRUAction::InsertionMode)
114 int QtxMRUAction::insertMode() const
120 \brief Set items insertion policy.
121 \param mode insertion policy (QtxMRUAction::InsertionMode)
123 void QtxMRUAction::setInsertMode( const int mode )
129 \brief Get the type of link menu name.
130 \return link type (QtxMRUAction::LinkType)
132 int QtxMRUAction::linkType() const
138 \brief Set the type of link menu name.
139 \param link type (QtxMRUAction::LinkType)
141 void QtxMRUAction::setLinkType( const int type )
147 \brief Get number of MRU items.
148 \return number of MRU items
150 int QtxMRUAction::count() const
152 return myLinks.count();
156 \brief Check if the MRU items list is empty.
157 \return \c true if there are no MRU items
159 bool QtxMRUAction::isEmpty() const
161 return myLinks.isEmpty();
165 \brief Get number of visible MRU items.
166 \return visible MRU items number
167 \sa setVisibleCount()
169 int QtxMRUAction::visibleCount() const
175 \brief Set number of visible MRU items.
177 This method sets the maximum number of MRU items
178 to be displayed in the popup menu (5 by default).
180 If \a num < 1, then all MRU items will be displayed.
182 \param num visible MRU items number
184 void QtxMRUAction::setVisibleCount( int num )
186 if ( myVisCount == num )
193 \brief Return visible status of the menu item which clear all MRU items.
195 bool QtxMRUAction::isClearPossible() const
197 return myClear->isVisible();
201 \brief Set visible the menu item which clear all MRU items.
203 void QtxMRUAction::setClearPossible( const bool on )
205 myClear->setVisible( on );
209 \brief Get number of totally stored MRU items.
210 \return number of MRU items stored in the preferences
211 \sa setHistoryCount(), saveLinks(), loadLinks()
213 int QtxMRUAction::historyCount() const
215 return myHistoryCount;
219 \brief Set number of totally stored MRU items.
221 This option allows setting number of MRU items to be stored
222 in the preferences file.
224 If \a num < 0, then number of stored MRU items is not limited.
226 \return number of MRU items stored in the preferences
227 \sa historyCount(), saveLinks(), loadLinks()
229 void QtxMRUAction::setHistoryCount( const int num )
231 myHistoryCount = num;
235 \brief Insert MRU item.
237 The item is inserted according to the current insertion policy.
239 \param link MRU item to be added
241 void QtxMRUAction::insert( const QString& link )
243 if ( myLinks.contains( link ) && ( insertMode() == AddFirst || insertMode() == AddLast ) )
246 myLinks.removeAll( link );
248 switch ( insertMode() )
252 myLinks.prepend( link );
256 myLinks.append( link );
262 \brief Remove MRU item.
264 Does nothing if \a idx is out of range.
266 \param idx MRU item index
268 void QtxMRUAction::remove( const int idx )
270 if ( idx < 0 || idx >= (int)myLinks.count() )
273 myLinks.removeAt( idx );
277 \brief Remove MRU item.
279 Does nothing if there is no speicified item in the list.
281 \param link MRU item to be removed
283 void QtxMRUAction::remove( const QString& link )
285 myLinks.removeAll( link );
289 \brief Remove all MRU items.
291 void QtxMRUAction::clear()
298 \param idx MRU item index
299 \return MRU item or null QString if \a idx is out of range
301 QString QtxMRUAction::item( const int idx ) const
304 if ( idx >= 0 && idx < (int)myLinks.count() )
310 \brief Get MRU item index.
312 \return MRU item index or -1 if item is not found
314 int QtxMRUAction::find( const QString& link ) const
316 return myLinks.indexOf( link );
320 \brief Check if MRU item is in the list.
322 \return \c true if specified item is already added to the list
324 bool QtxMRUAction::contains( const QString& link ) const
326 return myLinks.contains( link );
330 \brief Load the MRU items from specified resources section.
331 \param resMgr resources manager
332 \param section resources section
333 \param clear if \c true, previous MRU items list is cleared
335 void QtxMRUAction::loadLinks( QtxResourceMgr* resMgr, const QString& section, const bool clear )
337 if ( !resMgr || section.isEmpty() )
343 QString itemPrefix( "item_" );
345 QMap<QString, int> map;
346 for ( QStringList::const_iterator itr = myLinks.begin(); itr != myLinks.end(); ++ itr )
347 map.insert( *itr, 0 );
349 QStringList items = resMgr->parameters( section );
350 for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
352 if ( !(*it).startsWith( itemPrefix ) )
355 QString link = resMgr->stringValue( section, *it, QString() );
356 if ( link.isEmpty() || map.contains( link ) )
359 myLinks.append( link );
360 map.insert( link, 0 );
365 \brief Save the MRU items to specified resources section.
366 \param resMgr resources manager
367 \param section resources section
368 \param clear if \c true, the resources section is first cleared
370 void QtxMRUAction::saveLinks( QtxResourceMgr* resMgr, const QString& section, const bool clear ) const
372 if ( !resMgr || section.isEmpty() )
375 QString itemPrefix( "item_" );
378 QStringList items = resMgr->parameters( section );
379 for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
381 if ( (*it).startsWith( itemPrefix ) )
382 resMgr->remove( section, *it );
387 QMap<QString, int> map;
388 for ( QStringList::const_iterator itr = myLinks.begin(); itr != myLinks.end(); ++itr )
391 map.insert( *itr, 0 );
394 QStringList items = resMgr->parameters( section );
395 for ( QStringList::const_iterator it = items.begin(); it != items.end(); ++it )
397 if ( !(*it).startsWith( itemPrefix ) )
400 QString link = resMgr->stringValue( section, *it, QString() );
401 if ( !link.isEmpty() && !map.contains( link ) )
404 map.insert( link, 0 );
407 resMgr->remove( section, *it );
411 for ( QStringList::const_iterator iter = lst.begin();
412 iter != lst.end() && ( myHistoryCount < 0 || counter < myHistoryCount );
414 resMgr->setValue( section, itemPrefix + QString().sprintf( "%03d", counter ), *iter );
418 \brief Prepare MRU items popup menu.
420 This method is called when the parent menu is shown.
421 Enables or disables sub menu item according to the number of MRU items.
423 void QtxMRUAction::onAboutToShow()
429 \brief Called when any MRU item is selected by the user.
431 Emits signal activated(const QString&) passing selected MRU item as parameter.
433 void QtxMRUAction::onActivated()
435 QAction* a = ::qobject_cast<QAction*>( sender() );
439 QString link = a->data().toString();
440 if ( !link.isEmpty() && myLinks.contains( link ) )
441 emit activated( link );
444 void QtxMRUAction::onCleared( bool )
450 \brief Update MRU items popup menu.
452 void QtxMRUAction::updateMenu()
461 QMap<QString, int> map;
462 int count = visibleCount() < 0 ? myLinks.count() : visibleCount();
463 int i = insertMode() == AddLast || insertMode() == MoveLast ? qMax( 0, myLinks.count()-count ) : 0;
464 for ( ; i < myLinks.count() && count > 0; ++i, count-- )
466 links.append( myLinks[i] );
467 if ( linkType() == LinkAuto )
469 QString shortName = Qtx::file( myLinks[i] );
470 if ( map.contains( shortName ) )
473 map.insert( shortName, 0 );
478 for ( QStringList::const_iterator it = links.begin(); it != links.end(); ++it, i++ )
484 linkName = Qtx::file( *it );
485 if ( map.contains( linkName ) && map[linkName] )
489 linkName = Qtx::file( *it );
497 if ( links.count() < 10 )
498 linkName = QString( "&%1 %2" ).arg( i ).arg( linkName );
500 pm->addAction( linkName, this, SLOT( onActivated() ) )->setData( *it );
504 pm->addAction( tr( "<Empty>" ) )->setEnabled( false );
506 if ( isClearPossible() )
509 pm->addAction( myClear );
510 myClear->setEnabled( !pm->isEmpty() );
515 \fn void QtxMRUAction::activated( const QString& link );
516 \brief Emitted when user selects any MRU item in the menu.
517 \param link selected MRU item