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