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