1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "SUIT_Application.h"
21 #include "SUIT_Session.h"
22 #include "SUIT_Desktop.h"
23 #include "SUIT_ResourceMgr.h"
27 #include <qstatusbar.h>
28 #include <qapplication.h>
30 #include <QtxAction.h>
31 #include <QtxActionMenuMgr.h>
32 #include <QtxActionToolMgr.h>
37 SUIT_Application::SUIT_Application()
48 SUIT_Application::~SUIT_Application()
57 \return main window of application (desktop)
59 SUIT_Desktop* SUIT_Application::desktop()
65 \return FALSE if application can not be closed (because of non saved data for example).
66 This method called by SUIT_Session whin closing of application was requested.
68 bool SUIT_Application::isPossibleToClose( bool& )
74 Performs some finalization of life cycle of this application.
75 For instance, the application can force its documents(s) to close.
77 void SUIT_Application::closeApplication()
79 emit applicationClosed( this );
83 \return active Study. If Application supports wirking with several studies this method should be redefined
85 SUIT_Study* SUIT_Application::activeStudy() const
91 \return version of application
93 QString SUIT_Application::applicationVersion() const
99 Shows the application's main widget. For non GUI application must be redefined.
101 void SUIT_Application::start()
108 Opens document into active Study. If Study is empty - creates it.
109 \param theFileName - name of document file
111 bool SUIT_Application::useFile( const QString& theFileName )
114 SUIT_Study* study = activeStudy();
116 bool status = study ? study->openDocument( theFileName ) : false;
128 Opens other study into active Study. If Study is empty - creates it.
129 \param theName - name of study
131 bool SUIT_Application::useStudy( const QString& theName )
137 Creates new empty Study if active Study = 0
139 void SUIT_Application::createEmptyStudy()
141 if ( !activeStudy() )
142 setActiveStudy( createNewStudy() );
146 \return number of Studies.
147 Must be redefined in Applications which support several studies for one Application instance.
149 int SUIT_Application::getNbStudies() const
151 return activeStudy() ? 1 : 0;
155 \return global resource manager
157 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
159 if ( !SUIT_Session::session() )
162 return SUIT_Session::session()->resourceMgr();
165 #define DEFAULT_MESSAGE_DELAY 3000
168 Puts the message to the status bar
169 \param msg - text of message
170 \param msec - time in milliseconds, after that the status label will be cleared
172 void SUIT_Application::putInfo( const QString& msg, const int msec )
177 if ( !myStatusLabel )
179 myStatusLabel = new QLabel( desktop()->statusBar() );
180 desktop()->statusBar()->addWidget( myStatusLabel, 1 );
181 myStatusLabel->show();
184 QString prev = myStatusLabel->text();
186 myStatusLabel->setText( msg );
188 QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, this, SLOT( onInfoClear() ) );
191 emit infoChanged( msg );
195 Clear the information label in status bar after delay.
197 void SUIT_Application::onInfoClear()
199 if ( !myStatusLabel )
202 bool changed = !myStatusLabel->text().isEmpty();
203 myStatusLabel->clear();
205 emit infoChanged( QString::null );
209 Updates status of the registerd actions
211 void SUIT_Application::updateCommandsStatus()
215 Initialize with application arguments
216 \param argc - number of application arguments
217 \param argv - array of application arguments
219 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
221 return startApplication( name(), argc, argv );
225 Initialize with application name and arguments
226 \param name - name of application
227 \param argc - number of application arguments
228 \param argv - array of application arguments
230 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
232 SUIT_Session* session = SUIT_Session::session();
236 return session->startApplication( name, argc, argv );
240 Sets the main window of application
241 \param desk - new main window (desktop)
243 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
245 if ( myDesktop == desk )
251 connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
252 // Force desktop activation (NPAL16628)
253 QApplication::postEvent(myDesktop, new QEvent(QEvent::WindowActivate));
258 Creates new instance of study.
259 By default, it is called from createEmptyStudy()
260 \sa createEmptyStudy()
262 SUIT_Study* SUIT_Application::createNewStudy()
264 return new SUIT_Study( this );
269 \param study - instance of study to be set as active
271 void SUIT_Application::setActiveStudy( SUIT_Study* study )
273 if ( myStudy == study )
277 disconnect(myStudy, SIGNAL( studyModified( SUIT_Study* ) ), this, SLOT( updateCommandsStatus() ) );
280 connect(study, SIGNAL( studyModified( SUIT_Study* ) ), this, SLOT( updateCommandsStatus() ) );
287 \return identificator of new toolbar in tool manager
288 \param name - name of new toolbar
290 int SUIT_Application::createTool( const QString& name )
292 if ( !desktop() || !desktop()->toolMgr() )
295 return desktop()->toolMgr()->createToolBar( name );
299 Creates new toolbutton
300 \return SUIT identificator of new action
302 \param tBar - identificator of toolbar
303 \param id - proposed SUIT identificator of action (if it is -1, then must be use any free)
304 \param idx - index in toolbar
306 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
308 if ( !desktop() || !desktop()->toolMgr() )
311 int regId = registerAction( id, a );
312 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
313 return intId != -1 ? regId : -1;
317 Creates new toolbutton
318 \return SUIT identificator of new action
320 \param tBar - name of toolbar
321 \param id - proposed SUIT identificator of action (if it is -1, then must be use any free)
322 \param idx - index in toolbar
324 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
326 if ( !desktop() || !desktop()->toolMgr() )
329 int regId = registerAction( id, a );
330 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
331 return intId != -1 ? regId : -1;
335 Creates new toolbutton
336 \return "id" if all right or -1 otherwise
337 \param id - SUIT identificator of action
338 \param tBar - identificator of toolbar
339 \param idx - index in toolbar
341 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
343 if ( !desktop() || !desktop()->toolMgr() )
346 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
347 return intId != -1 ? id : -1;
351 Creates new toolbutton
352 \return "id" if all right or -1 otherwise
353 \param id - SUIT identificator of action
354 \param tBar - name of toolbar
355 \param idx - index in toolbar
357 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
359 if ( !desktop() || !desktop()->toolMgr() )
362 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
363 return intId != -1 ? id : -1;
367 Creates new menu item
368 \return identificator of new action in menu manager
369 \param subMenu - menu text of new item
370 \param menu - identificator of parent menu item
371 \param id - proposed identificator of action
372 \param group - group in menu manager
373 \param index - index in menu
375 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
376 const int id, const int group, const int index )
378 if ( !desktop() || !desktop()->menuMgr() )
381 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
385 Creates new menu item
386 \return identificator of new action in menu manager
387 \param subMenu - menu text of new item
388 \param menu - menu text of parent menu item
389 \param id - proposed identificator of action
390 \param group - group in menu manager
391 \param index - index in menu
393 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
394 const int id, const int group, const int index )
396 if ( !desktop() || !desktop()->menuMgr() )
399 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
403 Creates new menu item
404 \return SUIT identificator of new action
406 \param menu - identificator of parent menu item
407 \param id - proposed SUIT identificator of action
408 \param group - group in menu manager
409 \param index - index in menu
411 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
413 if ( !a || !desktop() || !desktop()->menuMgr() )
416 int regId = registerAction( id, a );
417 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
418 return intId != -1 ? regId : -1;
422 Creates new menu item
423 \return SUIT identificator of new action
425 \param menu - menu text of parent menu item
426 \param id - proposed SUIT identificator of action
427 \param group - group in menu manager
428 \param index - index in menu
430 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
432 if ( !a || !desktop() || !desktop()->menuMgr() )
435 int regId = registerAction( id, a );
436 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
437 return intId != -1 ? regId : -1;
441 Creates new menu item
442 \return identificator of new action in menu manager
443 \param id - SUIT identificator of action
444 \param menu - menu text of parent menu item
445 \param group - group in menu manager
446 \param index - index in menu
448 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
450 if ( !desktop() || !desktop()->menuMgr() )
453 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
454 return intId != -1 ? id : -1;
458 Creates new menu item
459 \return identificator of new action in menu manager
460 \param id - SUIT identificator of action
461 \param menu - menu text of parent menu item
462 \param group - group in menu manager
463 \param index - index in menu
465 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
467 if ( !desktop() || !desktop()->menuMgr() )
470 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
471 return intId != -1 ? id : -1;
475 Show/hide menu item corresponding to action
477 \param on - if it is true, the item will be shown, otherwise it will be hidden
479 void SUIT_Application::setMenuShown( QAction* a, const bool on )
481 setMenuShown( actionId( a ), on );
485 Show/hide menu item corresponding to action
486 \param id - identificator of action in menu manager
487 \param on - if it is true, the item will be shown, otherwise it will be hidden
489 void SUIT_Application::setMenuShown( const int id, const bool on )
491 if ( desktop() && desktop()->menuMgr() )
492 desktop()->menuMgr()->setShown( id, on );
496 Show/hide tool button corresponding to action
498 \param on - if it is true, the button will be shown, otherwise it will be hidden
500 void SUIT_Application::setToolShown( QAction* a, const bool on )
502 setToolShown( actionId( a ), on );
506 Show/hide menu item corresponding to action
507 \param id - identificator of action in tool manager
508 \param on - if it is true, the button will be shown, otherwise it will be hidden
510 void SUIT_Application::setToolShown( const int id, const bool on )
512 if ( desktop() && desktop()->toolMgr() )
513 desktop()->toolMgr()->setShown( id, on );
517 Show/hide both menu item and tool button corresponding to action
519 \param on - if it is true, the item will be shown, otherwise it will be hidden
521 void SUIT_Application::setActionShown( QAction* a, const bool on )
523 setMenuShown( a, on );
524 setToolShown( a, on );
528 Show/hide both menu item and tool button corresponding to action
529 \param id - identificator in both menu manager and tool manager
530 \param on - if it is true, the item will be shown, otherwise it will be hidden
532 void SUIT_Application::setActionShown( const int id, const bool on )
534 setMenuShown( id, on );
535 setToolShown( id, on );
539 \return action by it's SUIT identificator
540 \param id - SUIT identificator
542 QAction* SUIT_Application::action( const int id ) const
545 if ( myActionMap.contains( id ) )
551 \return SUIT identificator of action
554 int SUIT_Application::actionId( const QAction* a ) const
557 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin();
558 it != myActionMap.end() && id == -1;
560 if ( it.data() == a )
567 Creates action and registers it both in menu manager and tool manager
568 \return new instance of action
569 \param id - proposed SUIT identificator
570 \param text - description
571 \param icon - icon for toolbar
572 \param menu - menu text
573 \param tip - tool tip
574 \param key - shortcut
575 \param parent - parent object
576 \param toggle - if it is TRUE the action will be a toggle action, otherwise it will be a command action
577 \param reciever - object that contains slot
578 \param member - slot to be called when action is activated
580 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIconSet& icon,
581 const QString& menu, const QString& tip, const int key,
582 QObject* parent, const bool toggle, QObject* reciever, const char* member )
584 QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
585 a->setStatusTip( tip );
587 if ( reciever && member )
588 connect( a, SIGNAL( activated() ), reciever, member );
590 registerAction( id, a );
596 Registers action both in menu manager and tool manager
597 \param id - proposed SUIT identificator (if it is -1, auto generated one is used)
600 int SUIT_Application::registerAction( const int id, QAction* a )
602 int ident = actionId( a );
606 static int generatedId = -1;
607 ident = id == -1 ? --generatedId : id;
609 if ( action( ident ) )
610 qWarning( "Action registration id is already in use: %d", ident );
612 myActionMap.insert( ident, a );
614 if ( desktop() && desktop()->menuMgr() )
615 desktop()->menuMgr()->registerAction( a );
617 if ( desktop() && desktop()->toolMgr() )
618 desktop()->toolMgr()->registerAction( a );
624 \return global action used as separator
626 QAction* SUIT_Application::separator()
628 return QtxActionMgr::separator();
632 SLOT: it is called when desktop is activated
635 void SUIT_Application::onDesktopActivated()
637 emit activated( this );
641 SLOT: is used for Help browsing
644 void SUIT_Application::onHelpContextModule (const QString& /*theComponentName*/,
645 const QString& /*theFileName*/)