]> SALOME platform Git repositories - modules/gui.git/blob - src/STD/STD_Application.cxx
Salome HOME
bb93a11cf6ce95cb9708c0be4cc0753a36cfe92f
[modules/gui.git] / src / STD / STD_Application.cxx
1 #include "STD_Application.h"
2
3 #include "STD_MDIDesktop.h"
4
5 #include <SUIT_Tools.h>
6 #include <SUIT_Desktop.h>
7 #include <SUIT_Session.h>
8 #include <SUIT_ViewModel.h>
9 #include <SUIT_Operation.h>
10 #include <SUIT_MessageBox.h>
11 #include <SUIT_ResourceMgr.h>
12
13 #include <QtxDockAction.h>
14 #include <QtxActionMenuMgr.h>
15 #include <QtxActionToolMgr.h>
16 #include <QtxPopupMenu.h>
17
18 #include <qmenubar.h>
19 #include <qtoolbar.h>
20 #include <qpopupmenu.h>
21 #include <qstatusbar.h>
22 #include <qfiledialog.h>
23 #include <qapplication.h>
24
25 extern "C" STD_EXPORT SUIT_Application* createApplication()
26 {
27   return new STD_Application();
28 }
29
30 STD_Application::STD_Application()
31 : SUIT_Application(),
32 myEditEnabled( true ),
33 myActiveViewMgr( 0 )
34 {
35   STD_MDIDesktop* desk = new STD_MDIDesktop();
36
37   connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
38            this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
39
40   setDesktop( desk );
41 }
42
43 STD_Application::~STD_Application()
44 {
45 }
46
47 QString STD_Application::applicationName() const
48 {
49   return QString( "StdApplication" );
50 }
51
52 void STD_Application::start()
53 {
54   createActions();
55
56   updateDesktopTitle();
57   updateCommandsStatus();
58   setEditEnabled( myEditEnabled );
59
60   SUIT_Application::start();
61 }
62
63 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
64 {
65   if ( !isPossibleToClose() )
66   {
67     e->ignore();
68     return;
69   }
70
71   SUIT_Study* study = activeStudy();
72
73   if ( study )
74     study->closeDocument();
75
76   setActiveStudy( 0 );
77   delete study;
78
79   setDesktop( 0 );
80
81   closeApplication();
82 }
83
84 void STD_Application::createActions()
85 {
86   SUIT_Desktop* desk = desktop();
87   SUIT_ResourceMgr* resMgr = resourceMgr();
88   if ( !desk || !resMgr )
89     return;
90
91   // Create actions
92
93   createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
94                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
95                 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
96                 CTRL+Key_N, desk, false, this, SLOT( onNewDoc() ) );
97
98   createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
99                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
100                 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
101                 CTRL+Key_O, desk, false, this, SLOT( onOpenDoc() ) );
102
103   createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
104                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
105                 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
106                 CTRL+Key_W, desk, false, this, SLOT( onCloseDoc() ) );
107
108   createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIconSet(),
109                 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
110                 CTRL+Key_Q, desk, false, this, SLOT( onExit() ) );
111
112   createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
113                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
114                 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
115                 CTRL+Key_S, desk, false, this, SLOT( onSaveDoc() ) );
116
117   createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
118                 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
119                 0, desk, false, this, SLOT( onSaveAsDoc() ) );
120
121   createAction( EditCutId, tr( "TOT_DESK_EDIT_CUT" ),
122                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_CUT" ) ),
123                 tr( "MEN_DESK_EDIT_CUT" ), tr( "PRP_DESK_EDIT_CUT" ),
124                 CTRL+Key_X, desk, false, this, SLOT( onCut() ) );
125
126   createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
127                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
128                 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
129                 CTRL+Key_C, desk, false, this, SLOT( onCopy() ) );
130
131   createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
132                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
133                 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
134                 CTRL+Key_V, desk, false, this, SLOT( onPaste() ) );
135
136   QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
137                              QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
138                              tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
139   a->setOn( desk->statusBar()->isVisibleTo( desk ) );
140   connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
141
142   createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIconSet(),
143                 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk  );
144
145   createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
146                 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
147                 0, desk, false, this, SLOT( onHelpAbout() ) );
148
149   QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
150   registerAction( ViewWindowsId, da );
151   da->setAutoPlace( false );
152
153   // Create menus
154
155   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
156   int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
157   int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 10 );
158   int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
159
160   // Create menu items
161
162   createMenu( FileNewId, fileMenu, 0 );
163   createMenu( FileOpenId, fileMenu, 0 );
164   createMenu( FileCloseId, fileMenu, 0 );
165   createMenu( separator(), fileMenu, -1, 0 );
166   createMenu( FileSaveId, fileMenu, 0 );
167   createMenu( FileSaveAsId, fileMenu, 0 );
168   createMenu( separator(), fileMenu, -1, 0 );
169
170   createMenu( separator(), fileMenu );
171   createMenu( FileExitId, fileMenu );
172
173   createMenu( EditCutId, editMenu );
174   createMenu( EditCopyId, editMenu );
175   createMenu( EditPasteId, editMenu );
176   createMenu( separator(), editMenu );
177
178   createMenu( ViewWindowsId, viewMenu );
179   createMenu( ViewStatusBarId, viewMenu );
180   createMenu( separator(), viewMenu );
181
182   createMenu( HelpAboutId, helpMenu );
183   createMenu( separator(), helpMenu );
184
185   // Create tool bars
186
187   int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
188
189   // Create tool items
190
191   createTool( FileNewId, stdTBar );
192   createTool( FileOpenId, stdTBar );
193   createTool( FileSaveId, stdTBar );
194   createTool( FileCloseId, stdTBar );
195   createTool( separator(), stdTBar );
196   createTool( EditCutId, stdTBar );
197   createTool( EditCopyId, stdTBar );
198   createTool( EditPasteId, stdTBar );
199 }
200
201 /*!
202   Opens new application
203 */
204 void STD_Application::onNewDoc() 
205 {
206   if ( !activeStudy() )
207   {
208     createEmptyStudy();
209     activeStudy()->createDocument();
210     updateDesktopTitle();
211     updateCommandsStatus();
212   }
213   else
214   {
215     SUIT_Application* aApp = startApplication( 0, 0 );
216     if ( aApp->inherits( "STD_Application" ) )
217       ((STD_Application*)aApp)->onNewDoc();
218     else {
219       aApp->createEmptyStudy();
220       aApp->activeStudy()->createDocument();
221     }
222   }
223 }
224
225 void STD_Application::onOpenDoc()
226 {
227   // It is preferrable to use OS-specific file dialog box here !!!
228   QString aName = QFileDialog::getOpenFileName( QString::null, getFileFilter(), desktop() );
229   if ( aName.isNull() )
230     return;
231
232   if ( !activeStudy() )
233   {
234     // if no study - open in current desktop
235     createEmptyStudy();
236     activeStudy()->openDocument( aName );
237     updateDesktopTitle();
238     updateCommandsStatus();
239   }
240   else
241   {
242     // if study exists - open in new desktop. Check: is the same file is opened?
243     SUIT_Session* aSession = SUIT_Session::session();
244     QPtrList<SUIT_Application> aAppList = aSession->applications();
245     bool isAlreadyOpen = false;
246     SUIT_Application* aApp = 0;
247     for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
248     {
249       aApp = it.current();
250       if ( aApp->activeStudy()->studyName() == aName )
251         isAlreadyOpen = true;
252     }
253     if ( !isAlreadyOpen )
254     {
255       aApp = startApplication( 0, 0 );
256       if ( aApp )
257         aApp->useFile( aName );
258     }
259     else
260       aApp->desktop()->setActiveWindow();
261   }
262 }
263
264 void STD_Application::beforeCloseDoc( SUIT_Study* )
265 {
266 }
267
268 void STD_Application::afterCloseDoc()
269 {
270 }
271
272 void STD_Application::onCloseDoc()
273 {
274   if ( !isPossibleToClose() )
275     return;
276
277   SUIT_Study* study = activeStudy();
278
279   beforeCloseDoc( study );
280
281   QPtrListIterator<SUIT_ViewManager> it( myViewMgrs );
282   it.toLast();
283   SUIT_ViewManager* vm = 0;
284   while( ( vm = it.current()) != 0 )
285   {
286     vm->closeAllViews();
287     --it;
288   }
289
290   if ( study )
291     study->closeDocument();
292
293   setActiveStudy( 0 );
294   delete study;
295
296   int aNbStudies = 0;
297   QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
298   for ( unsigned i = 0; i < apps.count(); i++ )
299     aNbStudies += apps.at( i )->getNbStudies();
300
301   // STV: aNbStudies - number of currently existing studies (exclude currently closed)
302   // STV: aNbStudies should be compared with 0.
303   if ( aNbStudies )
304     setDesktop( 0 );
305   else
306   {
307     updateDesktopTitle();
308     updateCommandsStatus();
309   }
310
311   afterCloseDoc();
312
313   if ( !desktop() )
314     closeApplication();
315 }
316
317 bool STD_Application::isPossibleToClose()
318 {
319   if ( activeStudy() )
320   {
321     activeStudy()->abortAllOperations();
322     if ( activeStudy()->isModified() )
323     {
324       QString sName = activeStudy()->studyName().stripWhiteSpace();
325       QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
326       int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TOT_DESK_FILE_CLOSE" ), msg,
327                                             tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
328       switch ( aAnswer )
329       {
330       case 1:
331         if ( !activeStudy()->isSaved() )
332           if ( !onSaveAsDoc() )
333             return false;
334         else
335           onSaveDoc();
336         break;
337       case 2:
338         break;
339       case 3:
340       default:
341         return false;
342       }
343     }
344   }
345   return true;
346 }
347
348 void STD_Application::onSaveDoc()
349 {
350   if ( !activeStudy() )
351     return;
352
353   if ( activeStudy()->isSaved() )
354   {
355     activeStudy()->saveDocument();
356     updateCommandsStatus();
357   }
358   else
359     onSaveAsDoc();
360 }
361
362 bool STD_Application::onSaveAsDoc()
363 {
364   SUIT_Study* study = activeStudy();
365   if ( !study )
366     return false;
367
368   QString aName, aUsedFilter, anOldPath = study->studyName();
369   bool isSelected = false;
370   while( !isSelected )
371   {
372     // It is preferrable to use OS-specific file dialog box here !!!
373     aName = QFileDialog::getSaveFileName( anOldPath, getFileFilter(), desktop(),
374                                           0, QString::null, &aUsedFilter);
375
376     if ( aName.isNull() )
377       break;
378
379     if ( !getFileFilter().isNull() )
380     { // check exstension and add if it is necessary
381       int aStart = aUsedFilter.find('*');
382       int aEnd = aUsedFilter.find(')', aStart + 1);
383       QString aExt = aUsedFilter.mid(aStart + 1, aEnd - aStart - 1);
384       if (aExt.contains('*') == 0 ) {  // if it is not *.*
385         // Check that there is an extension at the end of the name
386         QString aNameTail = aName.right(aExt.length());
387           if ( aNameTail != aExt )
388             aName += aExt;
389       }
390     }
391     QFile aFile( aName );
392     if ( aFile.exists() )
393     {
394       int aAnswer = SUIT_MessageBox::warn2( desktop(), tr("TIT_FILE_SAVEAS"),
395                                                      tr("MSG_FILE_EXISTS").arg(aName),
396                                                      tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
397       if ( aAnswer != 2 )
398         isSelected = true;
399       else
400         anOldPath = aName; // Not to return to the same initial dir at each "while" step
401     }
402     else
403       isSelected = true;
404   }
405
406   if ( aName.isNull() ) 
407     return false;
408
409   study->saveDocumentAs( aName );
410   updateDesktopTitle();
411   updateCommandsStatus();
412   return true;
413 }
414
415 void STD_Application::onExit()
416 {
417   SUIT_Session::session()->closeSession();
418 }
419
420 void STD_Application::onCut()
421 {
422 }
423
424 void STD_Application::onCopy()
425 {
426 }
427
428 void STD_Application::onPaste()
429 {
430 }
431
432 void STD_Application::setEditEnabled( bool theEnable )
433 {
434   myEditEnabled = theEnable;
435
436   QtxActionMenuMgr* mMgr = desktop()->menuMgr();
437   QtxActionToolMgr* tMgr = desktop()->toolMgr();
438
439   for ( int i = EditCutId; i <= EditPasteId; i++ )
440   {
441     mMgr->setShown( i, myEditEnabled );
442     tMgr->setShown( i, myEditEnabled );
443   }
444 }
445
446 void STD_Application::useFile(const QString& theFileName)
447 {
448   SUIT_Application::useFile(theFileName);
449   updateDesktopTitle();
450   updateCommandsStatus();
451 }
452
453 void STD_Application::updateDesktopTitle()
454 {
455   QString aTitle = applicationName();
456   QString aVer = applicationVersion();
457   if ( !aVer.isEmpty() )
458     aTitle += QString( " " ) + aVer;
459
460   if ( activeStudy() )
461   {
462     QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
463     if ( !sName.isEmpty() )
464       aTitle += QString( " - [%1]" ).arg( sName );
465   }
466
467   desktop()->setCaption( aTitle );
468 }
469
470 void STD_Application::updateCommandsStatus()
471 {
472   bool aHasStudy = activeStudy() != 0;
473   bool aIsNeedToSave = false;
474   if ( aHasStudy ) 
475     aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
476
477   if ( action( FileSaveId ) )
478     action( FileSaveId )->setEnabled( aIsNeedToSave );
479   if ( action( FileSaveAsId ) )
480     action( FileSaveAsId )->setEnabled( aHasStudy );
481   if ( action( FileCloseId ) )
482     action( FileCloseId )->setEnabled( aHasStudy );
483   if ( action( NewWindowId ) )
484     action( NewWindowId )->setEnabled( aHasStudy );
485 }
486
487 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
488 {
489   SUIT_ViewManager* vm = 0;
490   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
491   {
492     if ( it.current()->getType() == vmType )
493       vm = it.current();
494   }
495   return vm;
496 }
497
498 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
499 {
500   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
501     if ( it.current()->getType() == vmType )
502       lst.append( it.current() );
503 }
504
505 void STD_Application::viewManagers( ViewManagerList& lst ) const
506 {
507   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
508     lst.append( it.current() );
509 }
510
511 ViewManagerList STD_Application::viewManagers() const
512 {
513   ViewManagerList lst;
514   viewManagers( lst );
515   return lst;
516 }
517
518 SUIT_ViewManager* STD_Application::activeViewManager() const
519 {
520   return myActiveViewMgr;
521 }
522
523 void STD_Application::addViewManager( SUIT_ViewManager* vm )
524 {
525   if ( !vm )
526     return;
527
528   if ( !containsViewManager( vm ) )
529   {
530     myViewMgrs.append( vm );
531     connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
532              this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
533     vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
534
535     emit viewManagerAdded( vm );
536   }
537 /*
538   if ( !activeViewManager() && myViewMgrs.count() == 1 )
539     setActiveViewManager( vm );
540 */
541 }
542
543 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
544 {
545   if ( !vm )
546     return;
547
548   vm->closeAllViews();
549
550   emit viewManagerRemoved( vm );
551
552   vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
553   vm->disconnect();
554   myViewMgrs.removeRef( vm );
555
556   if ( myActiveViewMgr == vm )
557     myActiveViewMgr = 0;
558 }
559
560 void STD_Application::clearViewManagers()
561 {
562   ViewManagerList lst;
563   viewManagers( lst );
564
565   for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
566     removeViewManager( it.current() );
567 }
568
569 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
570 {
571   return myViewMgrs.contains( vm ) > 0;
572 }
573
574 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
575 {
576   setActiveViewManager( vm );
577 }
578
579 void STD_Application::onViewStatusBar( bool on )
580 {
581   if ( on )
582     desktop()->statusBar()->show();
583   else
584     desktop()->statusBar()->hide();
585 }
586
587 void STD_Application::onHelpAbout()
588 {
589   SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
590 }
591
592 void STD_Application::createEmptyStudy()
593 {
594   SUIT_Application::createEmptyStudy();
595
596   SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
597
598   addViewManager( vm );
599 }
600
601 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
602 {
603   if ( !containsViewManager( vm ) )
604     return;
605
606   myActiveViewMgr = vm;
607   emit viewManagerActivated( vm );
608 }
609
610 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
611 {
612   QtxPopupMenu* popup = new QtxPopupMenu();
613   // fill popup by own items
614   QString title;
615   contextMenuPopup( client->popupClientType(), popup, title );
616   popup->setTitleText( title );
617
618   popup->insertSeparator();
619   // add items from popup client
620   client->contextMenuPopup( popup );
621   
622   SUIT_Tools::simplifySeparators( popup );
623
624   if ( popup->count() )
625     popup->exec( e->globalPos() );
626   delete popup;
627 }