1 // Copyright (C) 2007-2015 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 #include "SUIT_Application.h"
25 #include "SUIT_Study.h"
26 #include "SUIT_Session.h"
27 #include "SUIT_Desktop.h"
28 #include "SUIT_ResourceMgr.h"
29 #include "SUIT_ShortcutMgr.h"
34 #include <QApplication>
37 #include <QtxAction.h>
38 #include <QtxActionMenuMgr.h>
39 #include <QtxActionToolMgr.h>
44 \brief Status bar customization label. Used to workaroubd desktop resizing bug.
47 class StatusLabel : public QLabel
50 StatusLabel( QWidget* parent ) : QLabel( parent ) {}
51 QSize minimumSizeHint () const { return QSize( 0, QLabel::minimumSizeHint().height() ); }
57 SUIT_Application::SUIT_Application()
63 if ( SUIT_Session::session() )
64 SUIT_Session::session()->insertApplication( this );
70 SUIT_Application::~SUIT_Application()
72 SUIT_Study* s = myStudy;
80 \return main window of application (desktop)
82 SUIT_Desktop* SUIT_Application::desktop()
88 \return FALSE if application can not be closed (because of non saved data for example).
89 This method called by SUIT_Session whin closing of application was requested.
91 bool SUIT_Application::isPossibleToClose( bool& )
97 Performs some finalization of life cycle of this application.
98 For instance, the application can force its documents(s) to close.
100 void SUIT_Application::closeApplication()
102 emit applicationClosed( this );
106 \return active Study. If Application supports wirking with several studies this method should be redefined
108 SUIT_Study* SUIT_Application::activeStudy() const
114 \return version of application
116 QString SUIT_Application::applicationVersion() const
122 Shows the application's main widget. For non GUI application must be redefined.
124 void SUIT_Application::start()
129 // Initialize shortcut manager
130 SUIT_ShortcutMgr::Init();
134 Opens document into active Study. If Study is empty - creates it.
135 \param theFileName - name of document file
137 bool SUIT_Application::useFile( const QString& theFileName )
140 SUIT_Study* study = activeStudy();
142 bool status = study ? study->openDocument( theFileName ) : false;
154 Creates new empty Study if active Study = 0
156 void SUIT_Application::createEmptyStudy()
158 if ( !activeStudy() )
159 setActiveStudy( createNewStudy() );
163 \return number of Studies.
164 Must be redefined in Applications which support several studies for one Application instance.
166 int SUIT_Application::getNbStudies() const
168 return activeStudy() ? 1 : 0;
172 \return global resource manager
174 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
176 if ( !SUIT_Session::session() )
179 return SUIT_Session::session()->resourceMgr();
183 \brief Get access to shortcut manager.
184 \return global shortcut manager
186 SUIT_ShortcutMgr* SUIT_Application::shortcutMgr() const
188 return SUIT_ShortcutMgr::getShortcutMgr();
191 #define DEFAULT_MESSAGE_DELAY 3000
194 Puts the message to the status bar
195 \param msg - text of message
196 \param msec - time in milliseconds, after that the status label will be cleared
198 void SUIT_Application::putInfo( const QString& msg, const int msec )
203 if ( !myStatusLabel )
205 myStatusLabel = new StatusLabel( desktop()->statusBar() );
206 desktop()->statusBar()->addWidget( myStatusLabel, 1 );
207 myStatusLabel->show();
210 QString prev = myStatusLabel->text();
212 myStatusLabel->setText( msg );
214 QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, this, SLOT( onInfoClear() ) );
217 emit infoChanged( msg );
221 Clear the information label in status bar after delay.
223 void SUIT_Application::onInfoClear()
225 if ( !myStatusLabel )
228 bool changed = !myStatusLabel->text().isEmpty();
229 myStatusLabel->clear();
231 emit infoChanged( QString() );
235 Update status of the registerd actions
237 void SUIT_Application::updateCommandsStatus()
242 Initialize with application arguments
243 \param argc - number of application arguments
244 \param argv - array of application arguments
246 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
248 return startApplication( objectName(), argc, argv );
252 Initialize with application name and arguments
253 \param name - name of application
254 \param argc - number of application arguments
255 \param argv - array of application arguments
257 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
259 SUIT_Session* session = SUIT_Session::session();
263 return session->startApplication( name, argc, argv );
267 Sets the main window of application
268 \param desk - new main window (desktop)
270 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
272 if ( myDesktop == desk )
275 // >> VSR 09/06/2009: workaround about the Qt 4.5.0 bug: SIGSEGV on desktop delete
276 myDesktop->deleteLater(); // delete myDesktop;
280 connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
281 // Force desktop activation (NPAL16628)
282 QApplication::postEvent(myDesktop, new QEvent(QEvent::WindowActivate));
287 Creates new instance of study.
288 By default, it is called from createEmptyStudy()
289 \sa createEmptyStudy()
291 SUIT_Study* SUIT_Application::createNewStudy()
293 return new SUIT_Study( this );
298 \param study - instance of study to be set as active
300 void SUIT_Application::setActiveStudy( SUIT_Study* study )
302 if ( myStudy == study )
306 disconnect( myStudy, SIGNAL( studyModified( SUIT_Study* ) ),
307 this, SLOT( updateCommandsStatus() ) );
309 connect( study, SIGNAL( studyModified( SUIT_Study* ) ),
310 this, SLOT( updateCommandsStatus() ) );
317 \return identificator of new toolbar in tool manager
318 \param title - title of new toolbar
319 \param name - name (identifier) of new toolbar
321 int SUIT_Application::createTool( const QString& title, const QString& name )
323 if ( !desktop() || !desktop()->toolMgr() )
326 return desktop()->toolMgr()->createToolBar( title, name );
330 Creates new toolbutton
331 \return SUIT identificator of new action
333 \param tBar - identificator of toolbar
334 \param id - proposed SUIT identificator of action (if it is -1, then must be use any free)
335 \param idx - index in toolbar
337 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
339 if ( !desktop() || !desktop()->toolMgr() )
342 int regId = registerAction( id, a );
343 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
344 return intId != -1 ? regId : -1;
348 Creates new toolbutton
349 \return SUIT identificator of new action
351 \param tBar - name of toolbar
352 \param id - proposed SUIT identificator of action (if it is -1, then must be use any free)
353 \param idx - index in toolbar
355 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
357 if ( !desktop() || !desktop()->toolMgr() )
360 int regId = registerAction( id, a );
361 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
362 return intId != -1 ? regId : -1;
366 Creates new toolbutton
367 \return "id" if all right or -1 otherwise
368 \param id - SUIT identificator of action
369 \param tBar - identificator of toolbar
370 \param idx - index in toolbar
372 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
374 if ( !desktop() || !desktop()->toolMgr() )
377 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
378 return intId != -1 ? id : -1;
382 Creates new toolbutton
383 \return "id" if all right or -1 otherwise
384 \param id - SUIT identificator of action
385 \param tBar - name of toolbar
386 \param idx - index in toolbar
388 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
390 if ( !desktop() || !desktop()->toolMgr() )
393 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
394 return intId != -1 ? id : -1;
398 Creates new menu item
399 \return identificator of new action in menu manager
400 \param subMenu - menu text of new item
401 \param menu - identificator of parent menu item
402 \param id - proposed identificator of action
403 \param group - group in menu manager
404 \param index - index in menu
406 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
407 const int id, const int group, const int index )
409 if ( !desktop() || !desktop()->menuMgr() )
412 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
416 Creates new menu item
417 \return identificator of new action in menu manager
418 \param subMenu - menu text of new item
419 \param menu - menu text of parent menu item
420 \param id - proposed identificator of action
421 \param group - group in menu manager
422 \param index - index in menu
424 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
425 const int id, const int group, const int index )
427 if ( !desktop() || !desktop()->menuMgr() )
430 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
434 Creates new menu item
435 \return SUIT identificator of new action
437 \param menu - identificator of parent menu item
438 \param id - proposed SUIT identificator of action
439 \param group - group in menu manager
440 \param index - index in menu
442 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
444 if ( !a || !desktop() || !desktop()->menuMgr() )
447 int regId = registerAction( id, a );
448 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
449 return intId != -1 ? regId : -1;
453 Creates new menu item
454 \return SUIT identificator of new action
456 \param menu - menu text of parent menu item
457 \param id - proposed SUIT identificator of action
458 \param group - group in menu manager
459 \param index - index in menu
461 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
463 if ( !a || !desktop() || !desktop()->menuMgr() )
466 int regId = registerAction( id, a );
467 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
468 return intId != -1 ? regId : -1;
472 Creates new menu item
473 \return identificator of new action in menu manager
474 \param id - SUIT identificator of action
475 \param menu - menu text of parent menu item
476 \param group - group in menu manager
477 \param index - index in menu
479 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
481 if ( !desktop() || !desktop()->menuMgr() )
484 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
485 return intId != -1 ? id : -1;
489 Creates new menu item
490 \return identificator of new action in menu manager
491 \param id - SUIT identificator of action
492 \param menu - menu text of parent menu item
493 \param group - group in menu manager
494 \param index - index in menu
496 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
498 if ( !desktop() || !desktop()->menuMgr() )
501 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
502 return intId != -1 ? id : -1;
506 Show/hide menu item corresponding to action
508 \param on - if it is true, the item will be shown, otherwise it will be hidden
510 void SUIT_Application::setMenuShown( QAction* a, const bool on )
512 if ( !a || !desktop() )
515 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
517 mMgr->setShown( mMgr->actionId( a ), on );
521 Show/hide menu item corresponding to action
522 \param id - identificator of action in menu manager
523 \param on - if it is true, the item will be shown, otherwise it will be hidden
525 void SUIT_Application::setMenuShown( const int id, const bool on )
527 setMenuShown( action( id ), on );
531 Show/hide tool button corresponding to action
533 \param on - if it is true, the button will be shown, otherwise it will be hidden
535 void SUIT_Application::setToolShown( QAction* a, const bool on )
537 if ( !a || !desktop() )
540 QtxActionToolMgr* tMgr = desktop()->toolMgr();
542 tMgr->setShown( tMgr->actionId( a ), on );
546 Show/hide menu item corresponding to action
547 \param id - identificator of action in tool manager
548 \param on - if it is true, the button will be shown, otherwise it will be hidden
550 void SUIT_Application::setToolShown( const int id, const bool on )
552 setToolShown( action( id ), on );
556 Show/hide both menu item and tool button corresponding to action
558 \param on - if it is true, the item will be shown, otherwise it will be hidden
560 void SUIT_Application::setActionShown( QAction* a, const bool on )
562 setMenuShown( a, on );
563 setToolShown( a, on );
567 Show/hide both menu item and tool button corresponding to action
568 \param id - identificator in both menu manager and tool manager
569 \param on - if it is true, the item will be shown, otherwise it will be hidden
571 void SUIT_Application::setActionShown( const int id, const bool on )
573 setMenuShown( id, on );
574 setToolShown( id, on );
578 \return action by it's SUIT identificator
579 \param id - SUIT identificator
581 QAction* SUIT_Application::action( const int id ) const
584 if ( myActionMap.contains( id ) )
590 \return SUIT identificator of action
593 int SUIT_Application::actionId( const QAction* a ) const
596 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
598 if ( it.value() == a )
604 QList<QAction*> SUIT_Application::actions() const
606 return myActionMap.values();
609 QList<int> SUIT_Application::actionIds() const
611 return myActionMap.keys();
615 Creates action and registers it both in menu manager and tool manager
616 \return new instance of action
617 \param id - proposed SUIT identificator
618 \param text - description
619 \param icon - icon for toolbar
620 \param menu - menu text
621 \param tip - tool tip
622 \param key - shortcut
623 \param parent - parent object
624 \param toggle - if it is TRUE the action will be a toggle action, otherwise it will be a command action
625 \param reciever - object that contains slot
626 \param member - slot to be called when action is activated
628 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIcon& icon,
629 const QString& menu, const QString& tip, const int key,
630 QObject* parent, const bool toggle, QObject* reciever,
631 const char* member, const QString& shortcutAction )
633 QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle, shortcutAction );
634 a->setStatusTip( tip );
636 if ( reciever && member )
637 connect( a, SIGNAL( triggered( bool ) ), reciever, member );
639 registerAction( id, a );
645 Registers action both in menu manager and tool manager
646 \param id - proposed SUIT identificator (if it is -1, auto generated one is used)
649 int SUIT_Application::registerAction( const int id, QAction* a )
651 int ident = actionId( a );
655 static int generatedId = -1;
656 ident = id == -1 ? --generatedId : id;
658 if ( action( ident ) )
659 qWarning( "Action registration id is already in use: %d", ident );
661 myActionMap.insert( ident, a );
663 if ( desktop() && desktop()->menuMgr() )
664 desktop()->menuMgr()->registerAction( a );
666 if ( desktop() && desktop()->toolMgr() )
667 desktop()->toolMgr()->registerAction( a );
670 desktop()->addAction( a );
676 \return global action used as separator
678 QAction* SUIT_Application::separator()
680 return QtxActionMgr::separator();
684 SLOT: it is called when desktop is activated
687 void SUIT_Application::onDesktopActivated()
689 emit activated( this );
693 SLOT: is used for Help browsing
695 void SUIT_Application::onHelpContextModule( const QString& /*theComponentName*/,
696 const QString& /*theFileName*/,
697 const QString& /*theContext*/ )