Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SUIT / SUIT_Application.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "SUIT_Application.h"
20
21 #include "SUIT_Session.h"
22 #include "SUIT_Desktop.h"
23 #include "SUIT_ResourceMgr.h"
24
25 #include <qlabel.h>
26 #include <qtimer.h>
27 #include <qstatusbar.h>
28
29 #include <QtxAction.h>
30 #include <QtxActionMenuMgr.h>
31 #include <QtxActionToolMgr.h>
32
33 /*!
34   Default constructor
35 */
36 SUIT_Application::SUIT_Application()
37 : QObject( 0 ),
38 myStudy( 0 ),
39 myDesktop( 0 ),
40 myStatusLabel( 0 )
41
42 }
43
44 /*!
45   Destructor
46 */
47 SUIT_Application::~SUIT_Application() 
48 {
49   delete myStudy;
50   myStudy = 0;
51
52   setDesktop( 0 );
53 }
54
55 /*!
56   \return main window of application (desktop)
57 */
58 SUIT_Desktop* SUIT_Application::desktop()
59 {
60   return myDesktop;
61 }
62
63 /*!
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.
66 */
67 bool SUIT_Application::isPossibleToClose()
68 {
69   return true;
70 }
71
72 /*!
73   Performs some finalization of life cycle of this application.
74   For instance, the application can force its documents(s) to close.
75 */
76 void SUIT_Application::closeApplication()
77 {
78   emit applicationClosed( this );
79 }
80
81 /*!
82   \return active Study. If Application supports wirking with several studies this method should be redefined
83 */
84 SUIT_Study* SUIT_Application::activeStudy() const
85 {
86   return myStudy;
87 }
88
89 /*!
90   \return version of application
91 */
92 QString SUIT_Application::applicationVersion() const
93 {
94   return QString::null;
95 }
96
97 /*!
98   Shows the application's main widget. For non GUI application must be redefined.
99 */
100 void SUIT_Application::start()
101 {
102   if ( desktop() )
103     desktop()->show();
104 }
105
106 /*!
107   Opens document into active Study. If Study is empty - creates it.
108   \param theFileName - name of document file
109 */
110 bool SUIT_Application::useFile( const QString& theFileName )
111 {
112   createEmptyStudy();
113   SUIT_Study* study = activeStudy();
114
115   bool status = study ? study->openDocument( theFileName ) : false;
116
117   if ( !status )
118   {
119     setActiveStudy( 0 );
120     delete study;
121   }
122
123   return status;
124 }
125
126 /*!
127   Opens other study into active Study. If Study is empty - creates it.
128   \param theName - name of study
129 */
130 bool SUIT_Application::useStudy( const QString& theName )
131 {
132   return false;
133 }
134
135 /*!
136   Creates new empty Study if active Study = 0
137 */
138 void SUIT_Application::createEmptyStudy()
139 {
140   if ( !activeStudy() )
141     setActiveStudy( createNewStudy() );
142 }
143
144 /*!
145   \return number of Studies. 
146   Must be redefined in Applications which support several studies for one Application instance.
147 */
148 int SUIT_Application::getNbStudies() const
149 {
150   return activeStudy() ? 1 : 0;
151 }
152
153 /*!
154   \return global resource manager
155 */
156 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
157 {
158   if ( !SUIT_Session::session() )
159     return 0;
160
161   return SUIT_Session::session()->resourceMgr();
162 }
163
164 #define DEFAULT_MESSAGE_DELAY 3000
165
166 /*!
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
170 */
171 void SUIT_Application::putInfo ( const QString& msg, const int msec )
172 {
173   if ( !desktop() )
174     return;
175
176   if ( !myStatusLabel )
177   {
178     myStatusLabel = new QLabel( desktop()->statusBar() );
179     desktop()->statusBar()->addWidget( myStatusLabel, 1 );
180     myStatusLabel->show();
181   }
182
183   myStatusLabel->setText( msg );
184   if ( msec != -1 )
185     QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, myStatusLabel, SLOT( clear() ) );
186 }
187
188 /*!
189   Initialize with application arguments
190   \param argc - number of application arguments
191   \param argv - array of application arguments
192 */
193 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
194 {
195   return startApplication( name(), argc, argv );
196 }
197
198 /*!
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
203 */
204 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
205 {
206   SUIT_Session* session = SUIT_Session::session();
207   if ( !session )
208     return 0;
209
210   return session->startApplication( name, argc, argv );
211 }
212
213 /*!
214   Sets the main window of application
215   \param desk - new main window (desktop)
216 */
217 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
218 {
219   if ( myDesktop == desk )
220     return;
221
222   delete myDesktop;
223   myDesktop = desk;
224   if ( myDesktop )
225     connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
226 }
227
228 /*!
229   Creates new instance of study.
230   By default, it is called from createEmptyStudy()
231   \sa createEmptyStudy()
232 */
233 SUIT_Study* SUIT_Application::createNewStudy()
234 {
235   return new SUIT_Study( this );
236 }
237
238 /*!
239   Sets study as active
240   \param study - instance of study to be set as active
241 */
242 void SUIT_Application::setActiveStudy( SUIT_Study* study )
243 {
244   if ( myStudy == study )
245     return;
246
247   myStudy = study;
248 }
249
250 /*!
251   Creates new toolbar
252   \return identificator of new toolbar in tool manager
253   \param name - name of new toolbar
254 */
255 int SUIT_Application::createTool( const QString& name )
256 {
257   if ( !desktop() || !desktop()->toolMgr() )
258     return -1;
259
260   return desktop()->toolMgr()->createToolBar( name );
261 }
262
263 /*!
264   Creates new toolbutton
265   \return SUIT identificator of new action
266   \param a - 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
270 */
271 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
272 {
273   if ( !desktop() || !desktop()->toolMgr() )
274     return -1;
275
276   int regId = registerAction( id, a );
277   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
278   return intId != -1 ? regId : -1;
279 }
280
281 /*!
282   Creates new toolbutton
283   \return SUIT identificator of new action
284   \param a - 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
288 */
289 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
290 {
291   if ( !desktop() || !desktop()->toolMgr() )
292     return -1;
293
294   int regId = registerAction( id, a );
295   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
296   return intId != -1 ? regId : -1;
297 }
298
299 /*!
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
305 */
306 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
307 {
308   if ( !desktop() || !desktop()->toolMgr() )
309     return -1;
310
311   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
312   return intId != -1 ? id : -1;
313 }
314
315 /*!
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
321 */
322 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
323 {
324   if ( !desktop() || !desktop()->toolMgr() )
325     return -1;
326
327   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
328   return intId != -1 ? id : -1;
329 }
330
331 /*!
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
339 */
340 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
341                                   const int id, const int group, const int index )
342 {
343   if ( !desktop() || !desktop()->menuMgr() )
344     return -1;
345
346   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
347 }
348
349 /*!
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
357 */
358 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
359                                   const int id, const int group, const int index )
360 {
361   if ( !desktop() || !desktop()->menuMgr() )
362     return -1;
363
364   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
365 }
366
367 /*!
368   Creates new menu item
369   \return SUIT identificator of new action
370   \param a - 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
375 */
376 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
377 {
378   if ( !a || !desktop() || !desktop()->menuMgr() )
379     return -1;
380
381   int regId = registerAction( id, a );
382   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
383   return intId != -1 ? regId : -1;
384 }
385
386 /*!
387   Creates new menu item
388   \return SUIT identificator of new action
389   \param a - 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
394 */
395 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
396 {
397   if ( !a || !desktop() || !desktop()->menuMgr() )
398     return -1;
399
400   int regId = registerAction( id, a );
401   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
402   return intId != -1 ? regId : -1;
403 }
404
405 /*!
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
412 */
413 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
414 {
415   if ( !desktop() || !desktop()->menuMgr() )
416     return -1;
417
418   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
419   return intId != -1 ? id : -1;
420 }
421
422 /*!
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
429 */
430 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
431 {
432   if ( !desktop() || !desktop()->menuMgr() )
433     return -1;
434
435   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
436   return intId != -1 ? id : -1;
437 }
438
439 /*!
440   Show/hide menu item corresponding to action
441   \param a - action
442   \param on - if it is true, the item will be shown, otherwise it will be hidden
443 */
444 void SUIT_Application::setMenuShown( QAction* a, const bool on )
445 {
446   setMenuShown( actionId( a ), on );
447 }
448
449 /*!
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
453 */
454 void SUIT_Application::setMenuShown( const int id, const bool on )
455 {
456   if ( desktop() && desktop()->menuMgr() )
457     desktop()->menuMgr()->setShown( id, on );
458 }
459
460 /*!
461   Show/hide tool button corresponding to action
462   \param a - action
463   \param on - if it is true, the button will be shown, otherwise it will be hidden
464 */
465 void SUIT_Application::setToolShown( QAction* a, const bool on )
466 {
467   setToolShown( actionId( a ), on );
468 }
469
470 /*!
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
474 */
475 void SUIT_Application::setToolShown( const int id, const bool on )
476 {
477   if ( desktop() && desktop()->toolMgr() )
478     desktop()->toolMgr()->setShown( id, on );
479 }
480
481 /*!
482   Show/hide both menu item and tool button corresponding to action
483   \param a - action
484   \param on - if it is true, the item will be shown, otherwise it will be hidden
485 */
486 void SUIT_Application::setActionShown( QAction* a, const bool on )
487 {
488   setMenuShown( a, on );
489   setToolShown( a, on );
490 }
491
492 /*!
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
496 */
497 void SUIT_Application::setActionShown( const int id, const bool on )
498 {
499   setMenuShown( id, on );
500   setToolShown( id, on );
501 }
502
503 /*!
504   \return action by it's SUIT identificator
505   \param id - SUIT identificator
506 */
507 QAction* SUIT_Application::action( const int id ) const
508 {
509   QAction* a = 0;
510   if ( myActionMap.contains( id ) )
511     a = myActionMap[id];
512   return a;
513 }
514
515 /*!
516   \return SUIT identificator of action
517   \param a - action
518 */
519 int SUIT_Application::actionId( const QAction* a ) const
520 {
521   int id = -1;
522   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); 
523         it != myActionMap.end() && id == -1;
524         ++it ) {
525     if ( it.data() == a )
526       id = it.key();
527   }
528   return id;
529 }
530
531 /*!
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
544 */
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 )
548 {
549   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
550   a->setStatusTip( tip );
551
552   if ( reciever && member )
553     connect( a, SIGNAL( activated() ), reciever, member );
554
555   registerAction( id, a );
556
557   return a;
558 }
559
560 /*!
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)
563   \param a - action
564 */
565 int SUIT_Application::registerAction( const int id, QAction* a )
566 {
567   int ident = actionId( a );
568   if ( ident != -1 )
569     return ident;
570
571   static int generatedId = -1;
572   ident = id == -1 ? --generatedId : id;
573
574   if ( action( ident ) ) 
575     qWarning( "Action registration id is already in use: %d", ident );
576
577   myActionMap.insert( ident, a );
578
579   if ( desktop() && desktop()->menuMgr() )
580     desktop()->menuMgr()->registerAction( a );
581
582   if ( desktop() && desktop()->toolMgr() )
583     desktop()->toolMgr()->registerAction( a );
584
585   return ident;
586 }
587
588 /*!
589   \return global action used as separator
590 */
591 QAction* SUIT_Application::separator()
592 {
593   return QtxActionMgr::separator();
594 }
595
596 /*!
597   SLOT: it is called when desktop is activated
598 */
599
600 void SUIT_Application::onDesktopActivated()
601 {
602   emit activated( this );
603 }