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