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