Salome HOME
Removed Cut operation
[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     createEmptyStudy();
235     res = activeStudy()->openDocument( aName );
236     updateDesktopTitle();
237     updateCommandsStatus();
238   }
239   else
240   {
241     // if study exists - open in new desktop. Check: is the same file is opened?
242     SUIT_Session* aSession = SUIT_Session::session();
243     QPtrList<SUIT_Application> aAppList = aSession->applications();
244     bool isAlreadyOpen = false;
245     SUIT_Application* aApp = 0;
246     for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
247     {
248       aApp = it.current();
249       if ( aApp->activeStudy()->studyName() == aName )
250         isAlreadyOpen = true;
251     }
252     if ( !isAlreadyOpen )
253     {
254       aApp = startApplication( 0, 0 );
255       if ( aApp )
256         res = aApp->useFile( aName );
257     }
258     else
259       aApp->desktop()->setActiveWindow();
260   }
261   return res;
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 = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
368
369   if ( aName.isNull() ) 
370     return false;
371   bool isOk = study->saveDocumentAs( aName );
372
373   updateDesktopTitle();
374   updateCommandsStatus();
375
376   return isOk;
377 }
378
379 void STD_Application::onExit()
380 {
381   SUIT_Session::session()->closeSession();
382 }
383
384 void STD_Application::onCopy()
385 {
386 }
387
388 void STD_Application::onPaste()
389 {
390 }
391
392 void STD_Application::setEditEnabled( bool theEnable )
393 {
394   myEditEnabled = theEnable;
395
396   QtxActionMenuMgr* mMgr = desktop()->menuMgr();
397   QtxActionToolMgr* tMgr = desktop()->toolMgr();
398
399   for ( int i = EditCopyId; i <= EditPasteId; i++ )
400   {
401     mMgr->setShown( i, myEditEnabled );
402     tMgr->setShown( i, myEditEnabled );
403   }
404 }
405
406 bool STD_Application::useFile(const QString& theFileName)
407 {
408   bool res = SUIT_Application::useFile(theFileName);
409   updateDesktopTitle();
410   updateCommandsStatus();
411   return res;
412 }
413
414 void STD_Application::updateDesktopTitle()
415 {
416   QString aTitle = applicationName();
417   QString aVer = applicationVersion();
418   if ( !aVer.isEmpty() )
419     aTitle += QString( " " ) + aVer;
420
421   if ( activeStudy() )
422   {
423     QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
424     if ( !sName.isEmpty() )
425       aTitle += QString( " - [%1]" ).arg( sName );
426   }
427
428   desktop()->setCaption( aTitle );
429 }
430
431 void STD_Application::updateCommandsStatus()
432 {
433   bool aHasStudy = activeStudy() != 0;
434   bool aIsNeedToSave = false;
435   if ( aHasStudy ) 
436     aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
437
438   if ( action( FileSaveId ) )
439     action( FileSaveId )->setEnabled( aIsNeedToSave );
440   if ( action( FileSaveAsId ) )
441     action( FileSaveAsId )->setEnabled( aHasStudy );
442   if ( action( FileCloseId ) )
443     action( FileCloseId )->setEnabled( aHasStudy );
444   if ( action( NewWindowId ) )
445     action( NewWindowId )->setEnabled( aHasStudy );
446 }
447
448 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
449 {
450   SUIT_ViewManager* vm = 0;
451   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
452   {
453     if ( it.current()->getType() == vmType )
454       vm = it.current();
455   }
456   return vm;
457 }
458
459 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
460 {
461   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
462     if ( it.current()->getType() == vmType )
463       lst.append( it.current() );
464 }
465
466 void STD_Application::viewManagers( ViewManagerList& lst ) const
467 {
468   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
469     lst.append( it.current() );
470 }
471
472 ViewManagerList STD_Application::viewManagers() const
473 {
474   ViewManagerList lst;
475   viewManagers( lst );
476   return lst;
477 }
478
479 SUIT_ViewManager* STD_Application::activeViewManager() const
480 {
481   return myActiveViewMgr;
482 }
483
484 void STD_Application::addViewManager( SUIT_ViewManager* vm )
485 {
486   if ( !vm )
487     return;
488
489   if ( !containsViewManager( vm ) )
490   {
491     myViewMgrs.append( vm );
492     connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
493              this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
494     vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
495
496     emit viewManagerAdded( vm );
497   }
498 /*
499   if ( !activeViewManager() && myViewMgrs.count() == 1 )
500     setActiveViewManager( vm );
501 */
502 }
503
504 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
505 {
506   if ( !vm )
507     return;
508
509   vm->closeAllViews();
510
511   emit viewManagerRemoved( vm );
512
513   vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
514   vm->disconnect();
515   myViewMgrs.removeRef( vm );
516
517   if ( myActiveViewMgr == vm )
518     myActiveViewMgr = 0;
519 }
520
521 void STD_Application::clearViewManagers()
522 {
523   ViewManagerList lst;
524   viewManagers( lst );
525
526   for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
527     removeViewManager( it.current() );
528 }
529
530 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
531 {
532   return myViewMgrs.contains( vm ) > 0;
533 }
534
535 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
536 {
537   setActiveViewManager( vm );
538 }
539
540 void STD_Application::onViewStatusBar( bool on )
541 {
542   if ( on )
543     desktop()->statusBar()->show();
544   else
545     desktop()->statusBar()->hide();
546 }
547
548 void STD_Application::onHelpAbout()
549 {
550   SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
551 }
552
553 void STD_Application::createEmptyStudy()
554 {
555   SUIT_Application::createEmptyStudy();
556
557   SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
558
559   addViewManager( vm );
560 }
561
562 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
563 {
564   if ( !containsViewManager( vm ) )
565     return;
566
567   myActiveViewMgr = vm;
568   emit viewManagerActivated( vm );
569 }
570
571 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
572 {
573   QtxPopupMenu* popup = new QtxPopupMenu();
574   // fill popup by own items
575   QString title;
576   contextMenuPopup( client->popupClientType(), popup, title );
577   popup->setTitleText( title );
578
579   popup->insertSeparator();
580   // add items from popup client
581   client->contextMenuPopup( popup );
582   
583   SUIT_Tools::simplifySeparators( popup );
584
585   if ( popup->count() )
586     popup->exec( e->globalPos() );
587   delete popup;
588 }
589
590 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters, 
591                                       const QString& caption, QWidget* parent )
592 {
593   if ( !parent )
594     parent = desktop();
595   if ( open ) 
596   {
597     return QFileDialog::getOpenFileName( initial, filters, parent, 0, caption );
598   }
599   else
600   {
601     QString aName;
602     QString aUsedFilter;
603     QString anOldPath = initial;
604
605     bool isOk = false;
606     while ( !isOk )
607     {
608       // It is preferrable to use OS-specific file dialog box here !!!
609       aName = QFileDialog::getSaveFileName( anOldPath, filters, parent,
610                                             0, caption, &aUsedFilter);
611
612       if ( aName.isNull() )
613         isOk = true;
614       else
615       {
616         if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
617         {
618           int aStart = aUsedFilter.find( '*' );
619           int aEnd = aUsedFilter.find( ')', aStart + 1 );
620           QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
621           if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
622           {
623             // Check that there is an extension at the end of the name
624             QString aNameTail = aName.right( aExt.length() );
625             if ( aNameTail != aExt )
626               aName += aExt;
627           }
628         }
629         if ( QFileInfo( aName ).exists() )
630         {
631           int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
632                                                 tr( "MSG_FILE_EXISTS" ).arg( aName ),
633                                                 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
634           if ( aAnswer == 3 ) {     // cancelled
635             aName = QString::null;
636             isOk = true;
637           }
638           else if ( aAnswer == 2 ) // not save to this file
639             anOldPath = aName;             // not to return to the same initial dir at each "while" step
640           else                     // overwrite the existing file
641             isOk = true;
642         }
643         else
644           isOk = true;
645       }
646     }
647     return aName;
648   }
649 }
650
651 QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
652 {
653   if ( !parent )
654     parent = desktop();
655   return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );
656 }