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 #include "SUIT_Application.h"
24 #include "SUIT_Study.h"
25 #include "SUIT_Session.h"
26 #include "SUIT_Desktop.h"
27 #include "SUIT_ResourceMgr.h"
32 #include <QApplication>
35 #include <QtxAction.h>
36 #include <QtxActionMenuMgr.h>
37 #include <QtxActionToolMgr.h>
41 \brief Status bar customization label. Used to workaroubd desktop resizing bug.
44 class StatusLabel : public QLabel
47 StatusLabel( QWidget* parent ) : QLabel( parent ) {}
48 QSize minimumSizeHint () const { return QSize( 0, QLabel::minimumSizeHint().height() ); }
54 SUIT_Application::SUIT_Application()
60 if ( SUIT_Session::session() )
61 SUIT_Session::session()->insertApplication( this );
67 SUIT_Application::~SUIT_Application()
69 SUIT_Study* s = myStudy;
77 \return main window of application (desktop)
79 SUIT_Desktop* SUIT_Application::desktop()
85 \return FALSE if application can not be closed (because of non saved data for example).
86 This method called by SUIT_Session whin closing of application was requested.
88 bool SUIT_Application::isPossibleToClose( bool& )
94 Performs some finalization of life cycle of this application.
95 For instance, the application can force its documents(s) to close.
97 void SUIT_Application::closeApplication()
99 emit applicationClosed( this );
103 \return active Study. If Application supports wirking with several studies this method should be redefined
105 SUIT_Study* SUIT_Application::activeStudy() const
111 \return version of application
113 QString SUIT_Application::applicationVersion() const
119 Shows the application's main widget. For non GUI application must be redefined.
121 void SUIT_Application::start()
128 Opens document into active Study. If Study is empty - creates it.
129 \param theFileName - name of document file
131 bool SUIT_Application::useFile( const QString& theFileName )
134 SUIT_Study* study = activeStudy();
136 bool status = study ? study->openDocument( theFileName ) : false;
148 Creates new empty Study if active Study = 0
150 void SUIT_Application::createEmptyStudy()
152 if ( !activeStudy() )
153 setActiveStudy( createNewStudy() );
157 \return number of Studies.
158 Must be redefined in Applications which support several studies for one Application instance.
160 int SUIT_Application::getNbStudies() const
162 return activeStudy() ? 1 : 0;
166 \return global resource manager
168 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
170 if ( !SUIT_Session::session() )
173 return SUIT_Session::session()->resourceMgr();
176 #define DEFAULT_MESSAGE_DELAY 3000
179 Puts the message to the status bar
180 \param msg - text of message
181 \param msec - time in milliseconds, after that the status label will be cleared
183 void SUIT_Application::putInfo( const QString& msg, const int msec )
188 if ( !myStatusLabel )
190 myStatusLabel = new StatusLabel( desktop()->statusBar() );
191 desktop()->statusBar()->addWidget( myStatusLabel, 1 );
192 myStatusLabel->show();
195 QString prev = myStatusLabel->text();
197 myStatusLabel->setText( msg );
199 QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, this, SLOT( onInfoClear() ) );
202 emit infoChanged( msg );
206 Clear the information label in status bar after delay.
208 void SUIT_Application::onInfoClear()
210 if ( !myStatusLabel )
213 bool changed = !myStatusLabel->text().isEmpty();
214 myStatusLabel->clear();
216 emit infoChanged( QString() );
220 Update status of the registerd actions
222 void SUIT_Application::updateCommandsStatus()
227 Initialize with application arguments
228 \param argc - number of application arguments
229 \param argv - array of application arguments
231 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
233 return startApplication( objectName(), argc, argv );
237 Initialize with application name and arguments
238 \param name - name of application
239 \param argc - number of application arguments
240 \param argv - array of application arguments
242 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
244 SUIT_Session* session = SUIT_Session::session();
248 return session->startApplication( name, argc, argv );
252 Sets the main window of application
253 \param desk - new main window (desktop)
255 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
257 if ( myDesktop == desk )
263 connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
264 // Force desktop activation (NPAL16628)
265 QApplication::postEvent(myDesktop, new QEvent(QEvent::WindowActivate));
270 Creates new instance of study.
271 By default, it is called from createEmptyStudy()
272 \sa createEmptyStudy()
274 SUIT_Study* SUIT_Application::createNewStudy()
276 return new SUIT_Study( this );
281 \param study - instance of study to be set as active
283 void SUIT_Application::setActiveStudy( SUIT_Study* study )
285 if ( myStudy == study )
289 disconnect( myStudy, SIGNAL( studyModified( SUIT_Study* ) ),
290 this, SLOT( updateCommandsStatus() ) );
292 connect( study, SIGNAL( studyModified( SUIT_Study* ) ),
293 this, SLOT( updateCommandsStatus() ) );
300 \return identificator of new toolbar in tool manager
301 \param name - name of new toolbar
303 int SUIT_Application::createTool( const QString& name )
305 if ( !desktop() || !desktop()->toolMgr() )
308 return desktop()->toolMgr()->createToolBar( name );
312 Creates new toolbutton
313 \return SUIT identificator of new action
315 \param tBar - identificator of toolbar
316 \param id - proposed SUIT identificator of action (if it is -1, then must be use any free)
317 \param idx - index in toolbar
319 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
321 if ( !desktop() || !desktop()->toolMgr() )
324 int regId = registerAction( id, a );
325 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
326 return intId != -1 ? regId : -1;
330 Creates new toolbutton
331 \return SUIT identificator of new action
333 \param tBar - name 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 QString& 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 "id" if all right or -1 otherwise
350 \param id - SUIT identificator of action
351 \param tBar - identificator of toolbar
352 \param idx - index in toolbar
354 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
356 if ( !desktop() || !desktop()->toolMgr() )
359 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
360 return intId != -1 ? id : -1;
364 Creates new toolbutton
365 \return "id" if all right or -1 otherwise
366 \param id - SUIT identificator of action
367 \param tBar - name of toolbar
368 \param idx - index in toolbar
370 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
372 if ( !desktop() || !desktop()->toolMgr() )
375 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
376 return intId != -1 ? id : -1;
380 Creates new menu item
381 \return identificator of new action in menu manager
382 \param subMenu - menu text of new item
383 \param menu - identificator of parent menu item
384 \param id - proposed identificator of action
385 \param group - group in menu manager
386 \param index - index in menu
388 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
389 const int id, const int group, const int index )
391 if ( !desktop() || !desktop()->menuMgr() )
394 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
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 - menu text 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 QString& 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 SUIT identificator of new action
419 \param menu - identificator of parent menu item
420 \param id - proposed SUIT identificator of action
421 \param group - group in menu manager
422 \param index - index in menu
424 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
426 if ( !a || !desktop() || !desktop()->menuMgr() )
429 int regId = registerAction( id, a );
430 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
431 return intId != -1 ? regId : -1;
435 Creates new menu item
436 \return SUIT identificator of new action
438 \param menu - menu text of parent menu item
439 \param id - proposed SUIT identificator of action
440 \param group - group in menu manager
441 \param index - index in menu
443 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
445 if ( !a || !desktop() || !desktop()->menuMgr() )
448 int regId = registerAction( id, a );
449 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
450 return intId != -1 ? regId : -1;
454 Creates new menu item
455 \return identificator of new action in menu manager
456 \param id - SUIT identificator of action
457 \param menu - menu text of parent menu item
458 \param group - group in menu manager
459 \param index - index in menu
461 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
463 if ( !desktop() || !desktop()->menuMgr() )
466 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
467 return intId != -1 ? id : -1;
471 Creates new menu item
472 \return identificator of new action in menu manager
473 \param id - SUIT identificator of action
474 \param menu - menu text of parent menu item
475 \param group - group in menu manager
476 \param index - index in menu
478 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
480 if ( !desktop() || !desktop()->menuMgr() )
483 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
484 return intId != -1 ? id : -1;
488 Show/hide menu item corresponding to action
490 \param on - if it is true, the item will be shown, otherwise it will be hidden
492 void SUIT_Application::setMenuShown( QAction* a, const bool on )
494 if ( !a || !desktop() )
497 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
499 mMgr->setShown( mMgr->actionId( a ), on );
503 Show/hide menu item corresponding to action
504 \param id - identificator of action in menu manager
505 \param on - if it is true, the item will be shown, otherwise it will be hidden
507 void SUIT_Application::setMenuShown( const int id, const bool on )
509 setMenuShown( action( id ), on );
513 Show/hide tool button corresponding to action
515 \param on - if it is true, the button will be shown, otherwise it will be hidden
517 void SUIT_Application::setToolShown( QAction* a, const bool on )
519 if ( !a || !desktop() )
522 QtxActionToolMgr* tMgr = desktop()->toolMgr();
524 tMgr->setShown( tMgr->actionId( a ), on );
528 Show/hide menu item corresponding to action
529 \param id - identificator of action in tool manager
530 \param on - if it is true, the button will be shown, otherwise it will be hidden
532 void SUIT_Application::setToolShown( const int id, const bool on )
534 setToolShown( action( id ), on );
538 Show/hide both menu item and tool button corresponding to action
540 \param on - if it is true, the item will be shown, otherwise it will be hidden
542 void SUIT_Application::setActionShown( QAction* a, const bool on )
544 setMenuShown( a, on );
545 setToolShown( a, on );
549 Show/hide both menu item and tool button corresponding to action
550 \param id - identificator in both menu manager and tool manager
551 \param on - if it is true, the item will be shown, otherwise it will be hidden
553 void SUIT_Application::setActionShown( const int id, const bool on )
555 setMenuShown( id, on );
556 setToolShown( id, on );
560 \return action by it's SUIT identificator
561 \param id - SUIT identificator
563 QAction* SUIT_Application::action( const int id ) const
566 if ( myActionMap.contains( id ) )
572 \return SUIT identificator of action
575 int SUIT_Application::actionId( const QAction* a ) const
578 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
580 if ( it.value() == a )
586 QList<QAction*> SUIT_Application::actions() const
588 return myActionMap.values();
591 QList<int> SUIT_Application::actionIds() const
593 return myActionMap.keys();
597 Creates action and registers it both in menu manager and tool manager
598 \return new instance of action
599 \param id - proposed SUIT identificator
600 \param text - description
601 \param icon - icon for toolbar
602 \param menu - menu text
603 \param tip - tool tip
604 \param key - shortcut
605 \param parent - parent object
606 \param toggle - if it is TRUE the action will be a toggle action, otherwise it will be a command action
607 \param reciever - object that contains slot
608 \param member - slot to be called when action is activated
610 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIcon& icon,
611 const QString& menu, const QString& tip, const int key,
612 QObject* parent, const bool toggle, QObject* reciever, const char* member )
614 QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle );
615 a->setStatusTip( tip );
617 if ( reciever && member )
618 connect( a, SIGNAL( triggered( bool ) ), reciever, member );
620 registerAction( id, a );
626 Registers action both in menu manager and tool manager
627 \param id - proposed SUIT identificator (if it is -1, auto generated one is used)
630 int SUIT_Application::registerAction( const int id, QAction* a )
632 int ident = actionId( a );
636 static int generatedId = -1;
637 ident = id == -1 ? --generatedId : id;
639 if ( action( ident ) )
640 qWarning( "Action registration id is already in use: %d", ident );
642 myActionMap.insert( ident, a );
644 if ( desktop() && desktop()->menuMgr() )
645 desktop()->menuMgr()->registerAction( a );
647 if ( desktop() && desktop()->toolMgr() )
648 desktop()->toolMgr()->registerAction( a );
651 desktop()->addAction( a );
657 \return global action used as separator
659 QAction* SUIT_Application::separator()
661 return QtxActionMgr::separator();
665 SLOT: it is called when desktop is activated
668 void SUIT_Application::onDesktopActivated()
670 emit activated( this );
674 SLOT: is used for Help browsing
676 void SUIT_Application::onHelpContextModule( const QString& /*theComponentName*/,
677 const QString& /*theFileName*/,
678 const QString& /*theContext*/ )