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>
29 #include <QtxAction.h>
30 #include <QtxActionMenuMgr.h>
31 #include <QtxActionToolMgr.h>
36 SUIT_Application::SUIT_Application()
47 SUIT_Application::~SUIT_Application()
56 \return main window of application (desktop)
58 SUIT_Desktop* SUIT_Application::desktop()
64 \return FALSE if application can not be closed (because of non saved data for example).
65 This method called by SUIT_Session whin closing of application was requested.
67 bool SUIT_Application::isPossibleToClose()
73 Performs some finalization of life cycle of this application.
74 For instance, the application can force its documents(s) to close.
76 void SUIT_Application::closeApplication()
78 emit applicationClosed( this );
82 \return active Study. If Application supports wirking with several studies this method should be redefined
84 SUIT_Study* SUIT_Application::activeStudy() const
90 \return version of application
92 QString SUIT_Application::applicationVersion() const
98 Shows the application's main widget. For non GUI application must be redefined.
100 void SUIT_Application::start()
107 Opens document into active Study. If Study is empty - creates it.
108 \param theFileName - name of document file
110 bool SUIT_Application::useFile( const QString& theFileName )
113 SUIT_Study* study = activeStudy();
115 bool status = study ? study->openDocument( theFileName ) : false;
127 Opens other study into active Study. If Study is empty - creates it.
128 \param theName - name of study
130 bool SUIT_Application::useStudy( const QString& theName )
136 Creates new empty Study if active Study = 0
138 void SUIT_Application::createEmptyStudy()
140 if ( !activeStudy() )
141 setActiveStudy( createNewStudy() );
145 \return number of Studies.
146 Must be redefined in Applications which support several studies for one Application instance.
148 int SUIT_Application::getNbStudies() const
150 return activeStudy() ? 1 : 0;
154 \return global resource manager
156 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
158 if ( !SUIT_Session::session() )
161 return SUIT_Session::session()->resourceMgr();
164 #define DEFAULT_MESSAGE_DELAY 3000
167 Puts the message to the status bar
168 \param msg - text of message
169 \param msec - time in milliseconds, after that the status label will be cleared
171 void SUIT_Application::putInfo ( const QString& msg, const int msec )
176 if ( !myStatusLabel )
178 myStatusLabel = new QLabel( desktop()->statusBar() );
179 desktop()->statusBar()->addWidget( myStatusLabel, 1 );
180 myStatusLabel->show();
183 myStatusLabel->setText( msg );
185 QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, myStatusLabel, SLOT( clear() ) );
189 Initialize with application arguments
190 \param argc - number of application arguments
191 \param argv - array of application arguments
193 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
195 return startApplication( name(), argc, argv );
199 Initialize with application name and arguments
200 \param name - name of application
201 \param argc - number of application arguments
202 \param argv - array of application arguments
204 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
206 SUIT_Session* session = SUIT_Session::session();
210 return session->startApplication( name, argc, argv );
214 Sets the main window of application
215 \param desk - new main window (desktop)
217 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
219 if ( myDesktop == desk )
225 connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
229 Creates new instance of study.
230 By default, it is called from createEmptyStudy()
231 \sa createEmptyStudy()
233 SUIT_Study* SUIT_Application::createNewStudy()
235 return new SUIT_Study( this );
240 \param study - instance of study to be set as active
242 void SUIT_Application::setActiveStudy( SUIT_Study* study )
244 if ( myStudy == study )
252 \return identificator of new toolbar in tool manager
253 \param name - name of new toolbar
255 int SUIT_Application::createTool( const QString& name )
257 if ( !desktop() || !desktop()->toolMgr() )
260 return desktop()->toolMgr()->createToolBar( name );
264 Creates new toolbutton
265 \return SUIT identificator of new action
267 \param tBar - identificator of toolbar
268 \param id - proposed SUIT identificator of action (if it is -1, then must be use any free)
269 \param idx - index in toolbar
271 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
273 if ( !desktop() || !desktop()->toolMgr() )
276 int regId = registerAction( id, a );
277 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
278 return intId != -1 ? regId : -1;
282 Creates new toolbutton
283 \return SUIT identificator of new action
285 \param tBar - name of toolbar
286 \param id - proposed SUIT identificator of action (if it is -1, then must be use any free)
287 \param idx - index in toolbar
289 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
291 if ( !desktop() || !desktop()->toolMgr() )
294 int regId = registerAction( id, a );
295 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
296 return intId != -1 ? regId : -1;
300 Creates new toolbutton
301 \return "id" if all right or -1 otherwise
302 \param id - SUIT identificator of action
303 \param tBar - identificator of toolbar
304 \param idx - index in toolbar
306 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
308 if ( !desktop() || !desktop()->toolMgr() )
311 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
312 return intId != -1 ? id : -1;
316 Creates new toolbutton
317 \return "id" if all right or -1 otherwise
318 \param id - SUIT identificator of action
319 \param tBar - name of toolbar
320 \param idx - index in toolbar
322 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
324 if ( !desktop() || !desktop()->toolMgr() )
327 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
328 return intId != -1 ? id : -1;
332 Creates new menu item
333 \return identificator of new action in menu manager
334 \param subMenu - menu text of new item
335 \param menu - identificator of parent menu item
336 \param id - proposed identificator of action
337 \param group - group in menu manager
338 \param index - index in menu
340 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
341 const int id, const int group, const int index )
343 if ( !desktop() || !desktop()->menuMgr() )
346 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
350 Creates new menu item
351 \return identificator of new action in menu manager
352 \param subMenu - menu text of new item
353 \param menu - menu text of parent menu item
354 \param id - proposed identificator of action
355 \param group - group in menu manager
356 \param index - index in menu
358 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
359 const int id, const int group, const int index )
361 if ( !desktop() || !desktop()->menuMgr() )
364 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
368 Creates new menu item
369 \return SUIT identificator of new action
371 \param menu - identificator of parent menu item
372 \param id - proposed SUIT identificator of action
373 \param group - group in menu manager
374 \param index - index in menu
376 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
378 if ( !a || !desktop() || !desktop()->menuMgr() )
381 int regId = registerAction( id, a );
382 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
383 return intId != -1 ? regId : -1;
387 Creates new menu item
388 \return SUIT identificator of new action
390 \param menu - menu text of parent menu item
391 \param id - proposed SUIT identificator of action
392 \param group - group in menu manager
393 \param index - index in menu
395 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
397 if ( !a || !desktop() || !desktop()->menuMgr() )
400 int regId = registerAction( id, a );
401 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
402 return intId != -1 ? regId : -1;
406 Creates new menu item
407 \return identificator of new action in menu manager
408 \param id - SUIT identificator of action
409 \param menu - menu text of parent menu item
410 \param group - group in menu manager
411 \param index - index in menu
413 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
415 if ( !desktop() || !desktop()->menuMgr() )
418 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
419 return intId != -1 ? id : -1;
423 Creates new menu item
424 \return identificator of new action in menu manager
425 \param id - SUIT identificator of action
426 \param menu - menu text of parent menu item
427 \param group - group in menu manager
428 \param index - index in menu
430 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
432 if ( !desktop() || !desktop()->menuMgr() )
435 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
436 return intId != -1 ? id : -1;
440 Show/hide menu item corresponding to action
442 \param on - if it is true, the item will be shown, otherwise it will be hidden
444 void SUIT_Application::setMenuShown( QAction* a, const bool on )
446 setMenuShown( actionId( a ), on );
450 Show/hide menu item corresponding to action
451 \param id - identificator of action in menu manager
452 \param on - if it is true, the item will be shown, otherwise it will be hidden
454 void SUIT_Application::setMenuShown( const int id, const bool on )
456 if ( desktop() && desktop()->menuMgr() )
457 desktop()->menuMgr()->setShown( id, on );
461 Show/hide tool button corresponding to action
463 \param on - if it is true, the button will be shown, otherwise it will be hidden
465 void SUIT_Application::setToolShown( QAction* a, const bool on )
467 setToolShown( actionId( a ), on );
471 Show/hide menu item corresponding to action
472 \param id - identificator of action in tool manager
473 \param on - if it is true, the button will be shown, otherwise it will be hidden
475 void SUIT_Application::setToolShown( const int id, const bool on )
477 if ( desktop() && desktop()->toolMgr() )
478 desktop()->toolMgr()->setShown( id, on );
482 Show/hide both menu item and tool button corresponding to action
484 \param on - if it is true, the item will be shown, otherwise it will be hidden
486 void SUIT_Application::setActionShown( QAction* a, const bool on )
488 setMenuShown( a, on );
489 setToolShown( a, on );
493 Show/hide both menu item and tool button corresponding to action
494 \param id - identificator in both menu manager and tool manager
495 \param on - if it is true, the item will be shown, otherwise it will be hidden
497 void SUIT_Application::setActionShown( const int id, const bool on )
499 setMenuShown( id, on );
500 setToolShown( id, on );
504 \return action by it's SUIT identificator
505 \param id - SUIT identificator
507 QAction* SUIT_Application::action( const int id ) const
510 if ( myActionMap.contains( id ) )
516 \return SUIT identificator of action
519 int SUIT_Application::actionId( const QAction* a ) const
522 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin();
523 it != myActionMap.end() && id == -1;
525 if ( it.data() == a )
532 Creates action and registers it both in menu manager and tool manager
533 \return new instance of action
534 \param id - proposed SUIT identificator
535 \param text - description
536 \param icon - icon for toolbar
537 \param menu - menu text
538 \param tip - tool tip
539 \param key - shortcut
540 \param parent - parent object
541 \param toggle - if it is TRUE the action will be a toggle action, otherwise it will be a command action
542 \param reciever - object that contains slot
543 \param member - slot to be called when action is activated
545 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIconSet& icon,
546 const QString& menu, const QString& tip, const int key,
547 QObject* parent, const bool toggle, QObject* reciever, const char* member )
549 QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
550 a->setStatusTip( tip );
552 if ( reciever && member )
553 connect( a, SIGNAL( activated() ), reciever, member );
555 registerAction( id, a );
561 Registers action both in menu manager and tool manager
562 \param id - proposed SUIT identificator (if it is -1, auto generated one is used)
565 int SUIT_Application::registerAction( const int id, QAction* a )
567 int ident = actionId( a );
571 static int generatedId = -1;
572 ident = id == -1 ? --generatedId : id;
574 if ( action( ident ) )
575 qWarning( "Action registration id is already in use: %d", ident );
577 myActionMap.insert( ident, a );
579 if ( desktop() && desktop()->menuMgr() )
580 desktop()->menuMgr()->registerAction( a );
582 if ( desktop() && desktop()->toolMgr() )
583 desktop()->toolMgr()->registerAction( a );
589 \return global action used as separator
591 QAction* SUIT_Application::separator()
593 return QtxActionMgr::separator();
597 SLOT: it is called when desktop is activated
600 void SUIT_Application::onDesktopActivated()
602 emit activated( this );