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