]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_Application.cxx
Salome HOME
e6400595ce50f490dbd8f6dd410701f565b0a0e5
[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 #include <qapplication.h>
29
30 #include <QtxAction.h>
31 #include <QtxActionMenuMgr.h>
32 #include <QtxActionToolMgr.h>
33
34 /*!
35   Default constructor
36 */
37 SUIT_Application::SUIT_Application()
38   : QObject( 0 ),
39     myStudy( 0 ),
40     myDesktop( 0 ),
41     myStatusLabel( 0 )
42 {
43 }
44
45 /*!
46   Destructor
47 */
48 SUIT_Application::~SUIT_Application() 
49 {
50   setActiveStudy( 0 );
51   delete myStudy;
52
53   setDesktop( 0 );
54 }
55
56 /*!
57   \return main window of application (desktop)
58 */
59 SUIT_Desktop* SUIT_Application::desktop()
60 {
61   return myDesktop;
62 }
63
64 /*!
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.
67 */
68 bool SUIT_Application::isPossibleToClose( bool& )
69 {
70   return true;
71 }
72
73 /*!
74   Performs some finalization of life cycle of this application.
75   For instance, the application can force its documents(s) to close.
76 */
77 void SUIT_Application::closeApplication()
78 {
79   emit applicationClosed( this );
80 }
81
82 /*!
83   \return active Study. If Application supports wirking with several studies this method should be redefined
84 */
85 SUIT_Study* SUIT_Application::activeStudy() const
86 {
87   return myStudy;
88 }
89
90 /*!
91   \return version of application
92 */
93 QString SUIT_Application::applicationVersion() const
94 {
95   return QString::null;
96 }
97
98 /*!
99   Shows the application's main widget. For non GUI application must be redefined.
100 */
101 void SUIT_Application::start()
102 {
103   if ( desktop() )
104     desktop()->show();
105 }
106
107 /*!
108   Opens document into active Study. If Study is empty - creates it.
109   \param theFileName - name of document file
110 */
111 bool SUIT_Application::useFile( const QString& theFileName )
112 {
113   createEmptyStudy();
114   SUIT_Study* study = activeStudy();
115
116   bool status = study ? study->openDocument( theFileName ) : false;
117
118   if ( !status )
119   {
120     setActiveStudy( 0 );
121     delete study;
122   }
123
124   return status;
125 }
126
127 /*!
128   Opens other study into active Study. If Study is empty - creates it.
129   \param theName - name of study
130 */
131 bool SUIT_Application::useStudy( const QString& theName )
132 {
133   return false;
134 }
135
136 /*!
137   Creates new empty Study if active Study = 0
138 */
139 void SUIT_Application::createEmptyStudy()
140 {
141   if ( !activeStudy() )
142     setActiveStudy( createNewStudy() );
143 }
144
145 /*!
146   \return number of Studies. 
147   Must be redefined in Applications which support several studies for one Application instance.
148 */
149 int SUIT_Application::getNbStudies() const
150 {
151   return activeStudy() ? 1 : 0;
152 }
153
154 /*!
155   \return global resource manager
156 */
157 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
158 {
159   if ( !SUIT_Session::session() )
160     return 0;
161
162   return SUIT_Session::session()->resourceMgr();
163 }
164
165 #define DEFAULT_MESSAGE_DELAY 3000
166
167 /*!
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
171 */
172 void SUIT_Application::putInfo( const QString& msg, const int msec )
173 {
174   if ( !desktop() )
175     return;
176
177   if ( !myStatusLabel )
178   {
179     myStatusLabel = new QLabel( desktop()->statusBar() );
180     desktop()->statusBar()->addWidget( myStatusLabel, 1 );
181     myStatusLabel->show();
182   }
183
184   QString prev = myStatusLabel->text();
185
186   myStatusLabel->setText( msg );
187   if ( msec != -1 )
188     QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, this, SLOT( onInfoClear() ) );
189
190   if ( prev != msg )
191     emit infoChanged( msg );
192 }
193
194 /*!
195   Clear the information label in status bar after delay.
196 */
197 void SUIT_Application::onInfoClear()
198 {
199   if ( !myStatusLabel )
200     return;
201
202   bool changed = !myStatusLabel->text().isEmpty();
203   myStatusLabel->clear();
204   if ( changed )
205     emit infoChanged( QString::null );
206 }
207
208 /*!
209   Updates status of the registerd actions
210 */
211 void SUIT_Application::updateCommandsStatus()
212 {}
213
214 /*!
215   Initialize with application arguments
216   \param argc - number of application arguments
217   \param argv - array of application arguments
218 */
219 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
220 {
221   return startApplication( name(), argc, argv );
222 }
223
224 /*!
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
229 */
230 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
231 {
232   SUIT_Session* session = SUIT_Session::session();
233   if ( !session )
234     return 0;
235
236   return session->startApplication( name, argc, argv );
237 }
238
239 /*!
240   Sets the main window of application
241   \param desk - new main window (desktop)
242 */
243 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
244 {
245   if ( myDesktop == desk )
246     return;
247
248   delete myDesktop;
249   myDesktop = desk;
250   if ( myDesktop ) {
251     connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
252     // Force desktop activation (NPAL16628)
253     QApplication::postEvent(myDesktop, new QEvent(QEvent::WindowActivate));
254   }
255 }
256
257 /*!
258   Creates new instance of study.
259   By default, it is called from createEmptyStudy()
260   \sa createEmptyStudy()
261 */
262 SUIT_Study* SUIT_Application::createNewStudy()
263 {
264   return new SUIT_Study( this );
265 }
266
267 /*!
268   Sets study as active
269   \param study - instance of study to be set as active
270 */
271 void SUIT_Application::setActiveStudy( SUIT_Study* study )
272 {
273   if ( myStudy == study )
274     return;
275
276   if(myStudy)
277     disconnect(myStudy, SIGNAL( studyModified( SUIT_Study* ) ), this, SLOT( updateCommandsStatus() ) );
278
279   if(study)
280     connect(study, SIGNAL( studyModified( SUIT_Study* ) ), this, SLOT( updateCommandsStatus() ) );
281             
282   myStudy = study;
283 }
284
285 /*!
286   Creates new toolbar
287   \return identificator of new toolbar in tool manager
288   \param name - name of new toolbar
289 */
290 int SUIT_Application::createTool( const QString& name )
291 {
292   if ( !desktop() || !desktop()->toolMgr() )
293     return -1;
294
295   return desktop()->toolMgr()->createToolBar( name );
296 }
297
298 /*!
299   Creates new toolbutton
300   \return SUIT identificator of new action
301   \param a - 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
305 */
306 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
307 {
308   if ( !desktop() || !desktop()->toolMgr() )
309     return -1;
310
311   int regId = registerAction( id, a );
312   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
313   return intId != -1 ? regId : -1;
314 }
315
316 /*!
317   Creates new toolbutton
318   \return SUIT identificator of new action
319   \param a - 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
323 */
324 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
325 {
326   if ( !desktop() || !desktop()->toolMgr() )
327     return -1;
328
329   int regId = registerAction( id, a );
330   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
331   return intId != -1 ? regId : -1;
332 }
333
334 /*!
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
340 */
341 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
342 {
343   if ( !desktop() || !desktop()->toolMgr() )
344     return -1;
345
346   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
347   return intId != -1 ? id : -1;
348 }
349
350 /*!
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
356 */
357 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
358 {
359   if ( !desktop() || !desktop()->toolMgr() )
360     return -1;
361
362   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
363   return intId != -1 ? id : -1;
364 }
365
366 /*!
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
374 */
375 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
376                                   const int id, const int group, const int index )
377 {
378   if ( !desktop() || !desktop()->menuMgr() )
379     return -1;
380
381   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
382 }
383
384 /*!
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
392 */
393 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
394                                   const int id, const int group, const int index )
395 {
396   if ( !desktop() || !desktop()->menuMgr() )
397     return -1;
398
399   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
400 }
401
402 /*!
403   Creates new menu item
404   \return SUIT identificator of new action
405   \param a - 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
410 */
411 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
412 {
413   if ( !a || !desktop() || !desktop()->menuMgr() )
414     return -1;
415
416   int regId = registerAction( id, a );
417   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
418   return intId != -1 ? regId : -1;
419 }
420
421 /*!
422   Creates new menu item
423   \return SUIT identificator of new action
424   \param a - 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
429 */
430 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
431 {
432   if ( !a || !desktop() || !desktop()->menuMgr() )
433     return -1;
434
435   int regId = registerAction( id, a );
436   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
437   return intId != -1 ? regId : -1;
438 }
439
440 /*!
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
447 */
448 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
449 {
450   if ( !desktop() || !desktop()->menuMgr() )
451     return -1;
452
453   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
454   return intId != -1 ? id : -1;
455 }
456
457 /*!
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
464 */
465 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
466 {
467   if ( !desktop() || !desktop()->menuMgr() )
468     return -1;
469
470   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
471   return intId != -1 ? id : -1;
472 }
473
474 /*!
475   Show/hide menu item corresponding to action
476   \param a - action
477   \param on - if it is true, the item will be shown, otherwise it will be hidden
478 */
479 void SUIT_Application::setMenuShown( QAction* a, const bool on )
480 {
481   setMenuShown( actionId( a ), on );
482 }
483
484 /*!
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
488 */
489 void SUIT_Application::setMenuShown( const int id, const bool on )
490 {
491   if ( desktop() && desktop()->menuMgr() )
492     desktop()->menuMgr()->setShown( id, on );
493 }
494
495 /*!
496   Show/hide tool button corresponding to action
497   \param a - action
498   \param on - if it is true, the button will be shown, otherwise it will be hidden
499 */
500 void SUIT_Application::setToolShown( QAction* a, const bool on )
501 {
502   setToolShown( actionId( a ), on );
503 }
504
505 /*!
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
509 */
510 void SUIT_Application::setToolShown( const int id, const bool on )
511 {
512   if ( desktop() && desktop()->toolMgr() )
513     desktop()->toolMgr()->setShown( id, on );
514 }
515
516 /*!
517   Show/hide both menu item and tool button corresponding to action
518   \param a - action
519   \param on - if it is true, the item will be shown, otherwise it will be hidden
520 */
521 void SUIT_Application::setActionShown( QAction* a, const bool on )
522 {
523   setMenuShown( a, on );
524   setToolShown( a, on );
525 }
526
527 /*!
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
531 */
532 void SUIT_Application::setActionShown( const int id, const bool on )
533 {
534   setMenuShown( id, on );
535   setToolShown( id, on );
536 }
537
538 /*!
539   \return action by it's SUIT identificator
540   \param id - SUIT identificator
541 */
542 QAction* SUIT_Application::action( const int id ) const
543 {
544   QAction* a = 0;
545   if ( myActionMap.contains( id ) )
546     a = myActionMap[id];
547   return a;
548 }
549
550 /*!
551   \return SUIT identificator of action
552   \param a - action
553 */
554 int SUIT_Application::actionId( const QAction* a ) const
555 {
556   int id = -1;
557   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); 
558         it != myActionMap.end() && id == -1;
559         ++it ) {
560     if ( it.data() == a )
561       id = it.key();
562   }
563   return id;
564 }
565
566 /*!
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
579 */
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 )
583 {
584   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
585   a->setStatusTip( tip );
586
587   if ( reciever && member )
588     connect( a, SIGNAL( activated() ), reciever, member );
589
590   registerAction( id, a );
591
592   return a;
593 }
594
595 /*!
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)
598   \param a - action
599 */
600 int SUIT_Application::registerAction( const int id, QAction* a )
601 {
602   int ident = actionId( a );
603   if ( ident != -1 )
604     return ident;
605
606   static int generatedId = -1;
607   ident = id == -1 ? --generatedId : id;
608
609   if ( action( ident ) ) 
610     qWarning( "Action registration id is already in use: %d", ident );
611
612   myActionMap.insert( ident, a );
613
614   if ( desktop() && desktop()->menuMgr() )
615     desktop()->menuMgr()->registerAction( a );
616
617   if ( desktop() && desktop()->toolMgr() )
618     desktop()->toolMgr()->registerAction( a );
619
620   return ident;
621 }
622
623 /*!
624   \return global action used as separator
625 */
626 QAction* SUIT_Application::separator()
627 {
628   return QtxActionMgr::separator();
629 }
630
631 /*!
632   SLOT: it is called when desktop is activated
633 */
634
635 void SUIT_Application::onDesktopActivated()
636 {
637   emit activated( this );
638 }
639
640 /*!
641   SLOT: is used for Help browsing
642 */
643
644 void SUIT_Application::onHelpContextModule (const QString& /*theComponentName*/,
645                                             const QString& /*theFileName*/)
646 {
647 }