]> SALOME platform Git repositories - modules/gui.git/blob - src/STD/STD_Application.cxx
Salome HOME
e3ef0126c26cbeb9654f63e4a82ca9812c5bdc83
[modules/gui.git] / src / STD / STD_Application.cxx
1 #include "STD_Application.h"
2
3 #include "STD_MDIDesktop.h"
4
5 #include "STD_CloseDlg.h"
6
7 #include <SUIT_Tools.h>
8 #include <SUIT_Desktop.h>
9 #include <SUIT_Session.h>
10 #include <SUIT_ViewModel.h>
11 #include <SUIT_Operation.h>
12 #include <SUIT_MessageBox.h>
13 #include <SUIT_ResourceMgr.h>
14
15 #include <QtxDockAction.h>
16 #include <QtxActionMenuMgr.h>
17 #include <QtxActionToolMgr.h>
18 #include <QtxPopupMenu.h>
19
20 #include <qmenubar.h>
21 #include <qtoolbar.h>
22 #include <qpopupmenu.h>
23 #include <qstatusbar.h>
24 #include <qfiledialog.h>
25 #include <qapplication.h>
26
27 #include <iostream>
28
29 /*!Create and return new instance of STD_Application*/
30 extern "C" STD_EXPORT SUIT_Application* createApplication()
31 {
32   return new STD_Application();
33 }
34
35 /*!Constructor.*/
36 STD_Application::STD_Application()
37 : SUIT_Application(),
38 myEditEnabled( true ),
39 myActiveViewMgr( 0 )
40 {
41   STD_MDIDesktop* desk = new STD_MDIDesktop();
42
43   setDesktop( desk );
44 }
45
46 /*!Destructor.*/
47 STD_Application::~STD_Application()
48 {
49 }
50
51 /*! \retval QString "StdApplication"*/
52 QString STD_Application::applicationName() const
53 {
54   return QString( "StdApplication" );
55 }
56
57 /*!Start STD_Application*/
58 void STD_Application::start()
59 {
60   createActions();
61
62   updateDesktopTitle();
63   updateCommandsStatus();
64   setEditEnabled( myEditEnabled );
65
66   SUIT_Application::start();
67 }
68
69 /*!Event on closing desktop*/
70 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
71 {
72   if (SUIT_Session::session()->applications().count() < 2) {
73     onExit();
74     return;
75   }
76
77   if ( !isPossibleToClose() )
78   {
79     e->ignore();
80     return;
81   }
82
83   SUIT_Study* study = activeStudy();
84
85   if ( study )
86     study->closeDocument();
87
88   setActiveStudy( 0 );
89   delete study;
90
91   setDesktop( 0 );
92
93   closeApplication();
94 }
95
96 /*!Create actions, menus and tools*/
97 void STD_Application::createActions()
98 {
99   SUIT_Desktop* desk = desktop();
100   SUIT_ResourceMgr* resMgr = resourceMgr();
101   if ( !desk || !resMgr )
102     return;
103
104   // Create actions
105
106   createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
107                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
108                 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
109                 CTRL+Key_N, desk, false, this, SLOT( onNewDoc() ) );
110
111   createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
112                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
113                 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
114                 CTRL+Key_O, desk, false, this, SLOT( onOpenDoc() ) );
115
116   createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
117                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
118                 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
119                 CTRL+Key_W, desk, false, this, SLOT( onCloseDoc() ) );
120
121   createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIconSet(),
122                 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
123                 CTRL+Key_Q, desk, false, this, SLOT( onExit() ) );
124
125   createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
126                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
127                 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
128                 CTRL+Key_S, desk, false, this, SLOT( onSaveDoc() ) );
129
130   createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
131                 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
132                 0, desk, false, this, SLOT( onSaveAsDoc() ) );
133
134   createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
135                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
136                 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
137                 CTRL+Key_C, desk, false, this, SLOT( onCopy() ) );
138
139   createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
140                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
141                 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
142                 CTRL+Key_V, desk, false, this, SLOT( onPaste() ) );
143
144   QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
145                              QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
146                              tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
147   a->setOn( desk->statusBar()->isVisibleTo( desk ) );
148   connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
149
150   createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIconSet(),
151                 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk  );
152
153   createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
154                 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
155                 0, desk, false, this, SLOT( onHelpAbout() ) );
156
157   //SRN: BugID IPAL9021, add an action "Load"
158   createAction( FileLoadId, tr( "TOT_DESK_FILE_LOAD" ),
159                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
160                 tr( "MEN_DESK_FILE_LOAD" ), tr( "PRP_DESK_FILE_LOAD" ),
161                 CTRL+Key_L, desk, false, this, SLOT( onLoadDoc() ) );
162   //SRN: BugID IPAL9021: End
163
164   QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
165   registerAction( ViewWindowsId, da );
166   da->setAutoPlace( false );
167
168   // Create menus
169
170   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
171   int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
172   int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 10 );
173   int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
174
175   // Create menu items
176
177   createMenu( FileNewId, fileMenu, 0 );
178   createMenu( FileOpenId, fileMenu, 0 );
179   createMenu( FileLoadId, fileMenu, 0 );  //SRN: BugID IPAL9021, add a menu item "Load"
180   createMenu( FileCloseId, fileMenu, 0 );
181   createMenu( separator(), fileMenu, -1, 0 );
182   createMenu( FileSaveId, fileMenu, 0 );
183   createMenu( FileSaveAsId, fileMenu, 0 );
184   createMenu( separator(), fileMenu, -1, 0 );
185
186   createMenu( separator(), fileMenu );
187   createMenu( FileExitId, fileMenu );
188
189   createMenu( EditCopyId, editMenu );
190   createMenu( EditPasteId, editMenu );
191   createMenu( separator(), editMenu );
192
193   createMenu( ViewWindowsId, viewMenu );
194   createMenu( ViewStatusBarId, viewMenu );
195   createMenu( separator(), viewMenu );
196
197   createMenu( HelpAboutId, helpMenu );
198   createMenu( separator(), helpMenu );
199
200   // Create tool bars
201
202   int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
203
204   // Create tool items
205
206   createTool( FileNewId, stdTBar );
207   createTool( FileOpenId, stdTBar );
208   createTool( FileSaveId, stdTBar );
209   createTool( FileCloseId, stdTBar );
210   createTool( separator(), stdTBar );
211   createTool( EditCopyId, stdTBar );
212   createTool( EditPasteId, stdTBar );
213 }
214
215 /*!Opens new application*/
216 void STD_Application::onNewDoc()
217 {
218   QApplication::setOverrideCursor( Qt::waitCursor );
219
220   if ( !activeStudy() )
221   {
222     createEmptyStudy();
223     activeStudy()->createDocument();
224     updateDesktopTitle();
225     updateCommandsStatus();
226   }
227   else
228   {
229     SUIT_Application* aApp = startApplication( 0, 0 );
230     if ( aApp->inherits( "STD_Application" ) )
231       ((STD_Application*)aApp)->onNewDoc();
232     else {
233       aApp->createEmptyStudy();
234       aApp->activeStudy()->createDocument();
235     }
236   }
237
238   QApplication::restoreOverrideCursor();
239 }
240
241 /*!Put file name from file dialog to onOpenDoc(const QString&) function*/
242 void STD_Application::onOpenDoc()
243 {
244   // It is preferrable to use OS-specific file dialog box here !!!
245   QString aName = getFileName( true, QString::null, getFileFilter(), QString::null, 0 );
246   if ( aName.isNull() )
247     return;
248
249   onOpenDoc( aName );
250 }
251
252 /*! \retval true, if document was opened successful, else false.*/
253 bool STD_Application::onOpenDoc( const QString& aName )
254 {
255   QApplication::setOverrideCursor( Qt::waitCursor );
256
257   bool res = true;
258   if ( !activeStudy() )
259   {
260     // if no study - open in current desktop
261     res = useFile( aName );
262   }
263   else
264   {
265     // if study exists - open in new desktop. Check: is the same file is opened?
266     SUIT_Session* aSession = SUIT_Session::session();
267     QPtrList<SUIT_Application> aAppList = aSession->applications();
268     bool isAlreadyOpen = false;
269     SUIT_Application* aApp = 0;
270     for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
271     {
272       aApp = it.current();
273       if ( aApp->activeStudy()->studyName() == aName )
274         isAlreadyOpen = true;
275     }
276     if ( !isAlreadyOpen )
277     {
278       aApp = startApplication( 0, 0 );
279       if ( aApp )
280         res = aApp->useFile( aName );
281       if ( !res )
282         aApp->closeApplication();
283     }
284     else
285       aApp->desktop()->setActiveWindow();
286   }
287
288   QApplication::restoreOverrideCursor();
289
290   return res;
291 }
292
293 /*! called on loading the existent study */
294 void STD_Application::onLoadDoc()
295 {
296 }
297
298 /*! \retval true, if document was loaded successful, else false.*/
299 bool STD_Application::onLoadDoc( const QString& aName )
300 {
301   bool res = true;
302   if ( !activeStudy() )
303   {
304     // if no study - load in current desktop
305     res = useStudy( aName );
306   }
307   else
308   {
309     // if study exists - load in new desktop. Check: is the same file is loaded?
310     SUIT_Session* aSession = SUIT_Session::session();
311     QPtrList<SUIT_Application> aAppList = aSession->applications();
312     bool isAlreadyOpen = false;
313     SUIT_Application* aApp = 0;
314     for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
315     {
316       aApp = it.current();
317       if ( aApp->activeStudy()->studyName() == aName )
318         isAlreadyOpen = true;
319     }
320     if ( !isAlreadyOpen )
321     {
322       aApp = startApplication( 0, 0 );
323       if ( aApp )
324         res = aApp->useStudy( aName );
325     }
326     else
327       aApp->desktop()->setActiveWindow();
328   }
329   return res;
330 }
331
332 /*!Virtual function. Not implemented here.*/
333 void STD_Application::beforeCloseDoc( SUIT_Study* )
334 {
335 }
336
337 /*!Virtual function. Not implemented here.*/
338 void STD_Application::afterCloseDoc()
339 {
340 }
341
342 /*!Close document, if it's possible.*/
343 void STD_Application::onCloseDoc( bool ask )
344 {
345   if ( ask && !isPossibleToClose() )
346     return;
347
348   SUIT_Study* study = activeStudy();
349
350   beforeCloseDoc( study );
351
352   if ( study )
353     study->closeDocument(myClosePermanently);
354
355   clearViewManagers();
356
357   setActiveStudy( 0 );
358   delete study;
359
360   int aNbStudies = 0;
361   QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
362   for ( unsigned i = 0; i < apps.count(); i++ )
363     aNbStudies += apps.at( i )->getNbStudies();
364
365   // STV: aNbStudies - number of currently existing studies (exclude currently closed)
366   // STV: aNbStudies should be compared with 0.
367   if ( aNbStudies )
368     setDesktop( 0 );
369   else
370   {
371     updateDesktopTitle();
372     updateCommandsStatus();
373   }
374
375   afterCloseDoc();
376
377   if ( !desktop() )
378     closeApplication();
379 }
380
381 /*!Check the application on closing.
382  * \retval true if possible, else false
383  */
384 bool STD_Application::isPossibleToClose()
385 {
386   myClosePermanently = true; //SRN: BugID: IPAL9021
387   if ( activeStudy() )
388   {
389     activeStudy()->abortAllOperations();
390     if ( activeStudy()->isModified() )
391     {
392       QString sName = activeStudy()->studyName().stripWhiteSpace();
393       QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
394
395       //SRN: BugID: IPAL9021: Begin
396       STD_CloseDlg dlg(desktop());
397       switch( dlg.exec() )
398       {
399       case 1:
400         if ( activeStudy()->isSaved() )
401           onSaveDoc();
402         else if ( !onSaveAsDoc() )
403           return false;
404         break;
405       case 2:
406         break;
407       case 3:
408               myClosePermanently = false;
409         break;
410       case 4:
411       default:
412         return false;
413       }
414      //SRN: BugID: IPAL9021: End
415     }
416   }
417   return true;
418 }
419
420 /*!Save document if all ok, else error message.*/
421 void STD_Application::onSaveDoc()
422 {
423   if ( !activeStudy() )
424     return;
425
426   bool isOk = false;
427   if ( activeStudy()->isSaved() )
428   {
429     putInfo( tr( "INF_DOC_SAVING" ) + activeStudy()->studyName() );
430
431     QApplication::setOverrideCursor( Qt::waitCursor );
432
433     isOk = activeStudy()->saveDocument();
434
435     QApplication::restoreOverrideCursor();
436
437     if ( !isOk )
438     {
439       putInfo( "" );
440       SUIT_MessageBox::error1( desktop(), tr( "TIT_FILE_SAVEAS" ),
441                                                  tr( "MSG_CANT_SAVE" ).arg( activeStudy()->studyName() ), tr( "BUT_OK" ) );
442     }
443     else
444       putInfo( tr( "INF_DOC_SAVED" ).arg( "" ) );
445   }
446
447   if ( isOk )
448     updateCommandsStatus();
449   else
450     onSaveAsDoc();
451 }
452
453 /*! \retval TRUE, if doument saved successful, else FALSE.*/
454 bool STD_Application::onSaveAsDoc()
455 {
456   SUIT_Study* study = activeStudy();
457   if ( !study )
458     return false;
459
460   bool isOk = false;
461   while ( !isOk )
462   {
463     QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
464     if ( aName.isNull() )
465       return false;
466
467     QApplication::setOverrideCursor( Qt::waitCursor );
468
469     putInfo( tr( "INF_DOC_SAVING" ) + aName );
470     isOk = study->saveDocumentAs( aName );
471
472     putInfo( isOk ? tr( "INF_DOC_SAVED" ).arg( aName ) : "" );
473
474     QApplication::restoreOverrideCursor();
475
476     if ( !isOk )
477       SUIT_MessageBox::error1( desktop(), tr( "ERROR" ),
478                              tr( "INF_DOC_SAVING_FAILS" ).arg( aName ), tr( "BUT_OK" ) );
479   }
480
481   updateDesktopTitle();
482   updateCommandsStatus();
483
484   return isOk;
485 }
486
487 /*!Closing session.*/
488 void STD_Application::onExit()
489 {
490   int aAnswer = SUIT_MessageBox::info2( desktop(), tr( "INF_DESK_EXIT" ), tr( "QUE_DESK_EXIT" ),
491                                         tr( "BUT_OK" ), tr( "BUT_CANCEL" ), 1, 2, 2 );
492   if ( aAnswer == 1 )
493     SUIT_Session::session()->closeSession();
494 }
495
496 /*!Virtual slot. Not implemented here.*/
497 void STD_Application::onCopy()
498 {
499 }
500
501 /*!Virtual slot. Not implemented here.*/
502 void STD_Application::onPaste()
503 {
504 }
505
506 /*!Sets \a theEnable for menu manager and for tool manager.*/
507 void STD_Application::setEditEnabled( bool theEnable )
508 {
509   myEditEnabled = theEnable;
510
511   QtxActionMenuMgr* mMgr = desktop()->menuMgr();
512   QtxActionToolMgr* tMgr = desktop()->toolMgr();
513
514   for ( int i = EditCopyId; i <= EditPasteId; i++ )
515   {
516     mMgr->setShown( i, myEditEnabled );
517     tMgr->setShown( i, myEditEnabled );
518   }
519 }
520
521 /*!\retval true, if document opened successful, else false.*/
522 bool STD_Application::useFile(const QString& theFileName)
523 {
524   bool res = SUIT_Application::useFile(theFileName);
525   updateDesktopTitle();
526   updateCommandsStatus();
527   return res;
528 }
529
530 /*!Update desktop title.*/
531 void STD_Application::updateDesktopTitle()
532 {
533   QString aTitle = applicationName();
534   QString aVer = applicationVersion();
535   if ( !aVer.isEmpty() )
536     aTitle += QString( " " ) + aVer;
537
538   if ( activeStudy() )
539   {
540     QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
541     if ( !sName.isEmpty() )
542       aTitle += QString( " - [%1]" ).arg( sName );
543   }
544
545   desktop()->setCaption( aTitle );
546 }
547
548 /*!Update commands status.*/
549 void STD_Application::updateCommandsStatus()
550 {
551   bool aHasStudy = activeStudy() != 0;
552   bool aIsNeedToSave = false;
553   if ( aHasStudy )
554     aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
555
556   if ( action( FileSaveId ) )
557     action( FileSaveId )->setEnabled( aIsNeedToSave );
558   if ( action( FileSaveAsId ) )
559     action( FileSaveAsId )->setEnabled( aHasStudy );
560   if ( action( FileCloseId ) )
561     action( FileCloseId )->setEnabled( aHasStudy );
562   if ( action( NewWindowId ) )
563     action( NewWindowId )->setEnabled( aHasStudy );
564 }
565
566 /*!\retval SUIT_ViewManager by viewer manager type name.*/
567 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
568 {
569   SUIT_ViewManager* vm = 0;
570   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
571   {
572     if ( it.current()->getType() == vmType )
573       vm = it.current();
574   }
575   return vm;
576 }
577
578 /*! \param vmType - input view manager type name
579  * \param lst - output list of view managers with types \a vmType.
580  */
581 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
582 {
583   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
584     if ( it.current()->getType() == vmType )
585       lst.append( it.current() );
586 }
587
588 /*!\param lst - output list of all view managers.*/
589 void STD_Application::viewManagers( ViewManagerList& lst ) const
590 {
591   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
592     lst.append( it.current() );
593 }
594
595 /*!\retval ViewManagerList - const list of all view managers.*/
596 ViewManagerList STD_Application::viewManagers() const
597 {
598   ViewManagerList lst;
599   viewManagers( lst );
600   return lst;
601 }
602
603 /*!\retval SUIT_ViewManager - return pointer to active view manager.*/
604 SUIT_ViewManager* STD_Application::activeViewManager() const
605 {
606   return myActiveViewMgr;
607 }
608
609 /*!Add view manager to view managers list, if it not already there.*/
610 void STD_Application::addViewManager( SUIT_ViewManager* vm )
611 {
612   if ( !vm )
613     return;
614
615   if ( !containsViewManager( vm ) )
616   {
617     myViewMgrs.append( vm );
618     connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
619              this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
620     vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
621
622     emit viewManagerAdded( vm );
623   }
624 /*
625   if ( !activeViewManager() && myViewMgrs.count() == 1 )
626     setActiveViewManager( vm );
627 */
628 }
629
630 /*!Remove view manager from view managers list.*/
631 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
632 {
633   if ( !vm )
634     return;
635
636   vm->closeAllViews();
637
638   emit viewManagerRemoved( vm );
639
640   vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
641   vm->disconnect();
642   myViewMgrs.removeRef( vm );
643
644   if ( myActiveViewMgr == vm )
645     myActiveViewMgr = 0;
646 }
647
648 /*!Remove all view managers from view managers list.*/
649 void STD_Application::clearViewManagers()
650 {
651   ViewManagerList lst;
652   viewManagers( lst );
653
654   for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
655     removeViewManager( it.current() );
656 }
657
658 /*!\retval TRUE, if view manager \a vm, already in view manager list (\a myViewMgrs).*/
659 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
660 {
661   return myViewMgrs.contains( vm ) > 0;
662 }
663
664 /*!Private slot, sets active manager to \vm, if \vm in view managers list.*/
665 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
666 {
667   setActiveViewManager( vm );
668 }
669
670 /*!Sets status bar show, if \on = true, else status bar hide.*/
671 void STD_Application::onViewStatusBar( bool on )
672 {
673   if ( on )
674     desktop()->statusBar()->show();
675   else
676     desktop()->statusBar()->hide();
677 }
678
679 /*!Call SUIT_MessageBox::info1(...) with about information.*/
680 void STD_Application::onHelpAbout()
681 {
682   SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
683 }
684
685 /*!Create empty study. \n
686  * Create new view manager and adding it to view managers list.
687  */
688 void STD_Application::createEmptyStudy()
689 {
690   SUIT_Application::createEmptyStudy();
691
692   SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
693
694   addViewManager( vm );
695 }
696
697 /*!Sets active manager to \vm, if \vm in view managers list.*/
698 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
699 {
700   if ( !containsViewManager( vm ) )
701     return;
702
703   myActiveViewMgr = vm;
704   emit viewManagerActivated( vm );
705 }
706
707 /*!Public slot. */
708 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
709 {
710   QtxPopupMenu* popup = new QtxPopupMenu();
711   // fill popup by own items
712   QString title;
713   contextMenuPopup( client->popupClientType(), popup, title );
714   popup->setTitleText( title );
715
716   popup->insertSeparator();
717   // add items from popup client
718   client->contextMenuPopup( popup );
719
720   SUIT_Tools::simplifySeparators( popup );
721
722   if ( popup->count() )
723     popup->exec( e->globalPos() );
724   delete popup;
725 }
726
727 #include <qregexp.h>
728
729 /*!\retval QString - return file name from dialog.*/
730 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters,
731                                       const QString& caption, QWidget* parent )
732 {
733   if ( !parent )
734     parent = desktop();
735   if ( open )
736   {
737     return QFileDialog::getOpenFileName( initial, filters, parent, 0, caption );
738   }
739   else
740   {
741     QString aName;
742     QString aUsedFilter;
743     QString anOldPath = initial;
744
745     bool isOk = false;
746     while ( !isOk )
747     {
748       // It is preferrable to use OS-specific file dialog box here !!!
749       aName = QFileDialog::getSaveFileName( anOldPath, filters, parent, 0, caption, &aUsedFilter );
750
751       if ( aName.isNull() )
752         isOk = true;
753       else
754       {
755               int aEnd = aUsedFilter.findRev( ')' );
756               int aStart = aUsedFilter.findRev( '(', aEnd );
757               QString wcStr = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
758
759         int idx = 0;
760         QStringList extList;
761         QRegExp rx( "[\b\\*]*\\.([\\w]+)" );
762         while ( ( idx = rx.search( wcStr, idx ) ) != -1 )
763         {
764           extList.append( rx.cap( 1 ) );
765           idx += rx.matchedLength();
766         }
767
768         if ( !extList.isEmpty() && !extList.contains( QFileInfo( aName ).extension() ) )
769           aName += QString( ".%1" ).arg( extList.first() );
770
771               if ( QFileInfo( aName ).exists() )
772         {
773                 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
774                                                                               tr( "MSG_FILE_EXISTS" ).arg( aName ),
775                                                                               tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
776                 if ( aAnswer == 3 )
777           {     // cancelled
778             aName = QString::null;
779                   isOk = true;
780           }
781                 else if ( aAnswer == 2 ) // not save to this file
782                   anOldPath = aName;             // not to return to the same initial dir at each "while" step
783                 else                     // overwrite the existing file
784                   isOk = true;
785         }
786               else
787                 isOk = true;
788       }
789     }
790     return aName;
791   }
792 }
793
794 /*!\retval QString - return directory name from dialog.*/
795 QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
796 {
797   if ( !parent )
798     parent = desktop();
799   return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );
800 }
801
802 void STD_Application::setDesktop( SUIT_Desktop* desk )
803 {
804   SUIT_Desktop* prev = desktop();
805
806   SUIT_Application::setDesktop( desk );
807
808   if ( prev != desk && desk )
809     connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
810              this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
811 }